关于从httpservletrequest中获取图片

NogardWolley NogardWolley 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
前端传了张图片到后台,request里面有图片,但存到list后list的size为0
怎样才能将request里的文件取出来

@RequestMapping("/picture")
public ResponseEntity uploadPicture(HttpServletRequest request, HttpServletResponse response) throws Exception {
//获取文件需要上传到的路径
String path = request.getRealPath("/upload") + "/";
File dir = new File(path);
if (!dir.exists()) {
dir.mkdir();
}
logger.debug("path=" + path);


request.setCharacterEncoding("utf-8"); //设置编码

DiskFileItemFactory factory = new DiskFileItemFactory();


factory.setRepository(dir);

factory.setSizeThreshold(1024 * 1024 * 1024 );

ServletFileUpload upload = new ServletFileUpload(factory);
//返回值设定
String ret="no result";
// List<FileItem> fileItemFromRequestList = getDataFromRequest(request);
try {
List<FileItem> list = upload.parseRequest(request);
FileItem picture = null;
System.out.println("i am here"+list);
for (FileItem item : list) {
//获取表单的属性名字
String name = item.getFieldName();
//如果获取的表单信息是普通的 文本 信息
if (item.isFormField()) {
//获取用户具体输入的字符串
String value = item.getString();
request.setAttribute(name, value);
logger.debug("name=" + name + ",value=" + value);
} else {
picture = item;
}
}
...全文
评论 1
收藏 已收藏
分享
返回顶部
NogardWolley
Java 51,407 成员 86,129 内容