关于从httpservletrequest中获取图片
前端传了张图片到后台,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;
}
}
怎样才能将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;
}
}
...全文
相关推荐
SpringMVC返回图片的几种方式(小结)
主要介绍了SpringMVC返回图片的几种方式(小结),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
SpringBoot实现前端验证码图片生成和校验
主要为大家详细介绍了SpringBoot实现前端验证码图片生成和校验,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
jsp实现上传图片功能
jsp+servlet+javabean实现图片上传功能,存放地址在数据库中
jsp上传图片代码
关于在jsp页面上传图片的代码, Apache commons-fileupload是一个很好的文件上传工具,最近使用commons-fileupload实现了图片的上传及显示,可将图片保存在指定的文件夹中,也可以将图片存放在数据库,并支持四种常用的图片格式:jpg,png,gif,bmp。
java实现图片上传预览
java实现图片上传预览,包含asp和jsp的实现代码,以及相关说明


