首页 新闻 会员 周边 捐助

关于文件上传解析结果List为空的问题:List<FileItem> list = upload.parseRequest(request);list=null;

0
悬赏园豆:10 [已关闭问题] 关闭于 2016-01-29 21:18

List<FileItem> list = upload.parseRequest(request);list=null; why???

Servlet里面的代码:

1 public String doUploadPicture(HttpServletRequest request){ 2 String result = "error"; 3 try{ 4 String filepath = getServletContext().getInitParameter("photopath");//配置文件里面配置了照片路径? 5 6 DiskFileItemFactory factory = new DiskFileItemFactory();// 创建文件项目工厂对象 7 factory.setRepository(new File(TEMP_FOLDER)); //设置临时文件夹为TEMP_FOLDER 8 factory.setSizeThreshold(1024 * 1024); // 设置缓冲区大小为 1M 9 ServletFileUpload upload = new ServletFileUpload(factory);//用工厂实例化上传组件,ServletFileUpload用来解析文件上传请求 10 11 List<FileItem> list = upload.parseRequest(request);// 解析结果放在List中 12 13 FileItem item = getUploadFileItem(list); // 获取上传的文件 14 String filename = getUploadFileName(item); // 获取文件名 15 filename = filename.substring(filename.length() - 3, filename.length()); //取得原文件名的后缀名 16 String photo_prex = "." + filename; 17 Date date = new Date(); 18 SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); 19 String time = df.format(date); 20 filename = time + photo_prex; //修改原来的文件名 21 // 真正写到磁盘上 22 item.write(new File(filepath, filename)); // 第三方提供的 23 //调用服务层插入一条数据 24 result = filename; 25 }catch(Exception e){ 26 result = "error"; 27 }finally{ 28 return result; 29 } 30 }
  JAVASCRIPT代码:
 1 $("#ajaxUpload").click(function(){
 2         var title = $.trim($("#pic_title").val());
 3         if(title == ""){
 4             alert("请输入文件名!");
 5             return false;}
 6         
 7         var type = $(":input[type='file']").val().match(/^(.*)(\.)(.{1,8})$/)[3];
 8         type = type.toUpperCase();
 9         if (type == "JPG" || type == "PNG" ) {
10         } else { alert("上传类型有误,请上传jpg或png格式图片文件"); return false; }
11             
12         $.ajaxFileUpload({
13             url :'<%= server_path%>/UploadAction?action=uploadPicture', 
14             secureuri : false,
15             fileElementId : 'fileToUpload',
16             dataType : 'json',
17             success : function(dataResult, status) {
18                 if(dataResult.result == "ok" && status == "success" && dataResult.returnValue != ""){
19                     alert("success");
20                     var path = dataResult.returnValue;
21                     var url = "<%= server_path%>/UserAction?action=doAddPicture";
22                     var json = {
23                             "userid" : userid,
24                             "path" : path,
25                             "title" : title,
26                     }
27                     var dataResult  = postAjax(url, dataResult);
28                     if(dataResult.result == "ok"){
29                         alert("上传成功!");
30                         showUserPicture(path,title);
31                     }else{alert("失败!");}
32                     
33                 }
34             },
35             error : function(data,status,e){
36                 alert("上传出错!")
37             } 
38         })
39         return false;//?
40     })

 

 
View Code
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册