转:upload.parseRequest为空
生活随笔
收集整理的這篇文章主要介紹了
转:upload.parseRequest为空
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
List items = upload.parseRequest(request); ? MultiPartRequestWrapper?wrapper?=?(MultiPartRequestWrapper)?request;??? ????????File?file?=?wrapper.getFiles("imgFile")[0];??? ????????String?fileName?=?wrapper.getFileNames("imgFile")[0];?? ????????//檢查文件大小?? ????????if(file.length()?>?maxSize){?? ????????????String?temStr=?"上傳文件大小超過限制。";?? ????????????this.writeResponse(response,?temStr);?? ????????????return;?? ????????}?? ????????//檢查擴展名?? ????????String?fileExt?=?fileName.substring(fileName.lastIndexOf(".")?+?1).toLowerCase();?? ????????if(!Arrays.<String>asList(extMap.get(dirName).split(",")).contains(fileExt)){?? ????????????String?temStr=?"上傳文件擴展名是不允許的擴展名。\n只允許"?+?extMap.get(dirName)?+?"格式。";?? ????????????this.writeResponse(response,?temStr);?? ????????????return;?? ????????}?? ?? ????????SimpleDateFormat?df?=?new?SimpleDateFormat("yyyyMMddHHmmss");?? ????????String?newFileName?=?df.format(new?Date())?+?"_"?+?new?Random().nextInt(1000)?+?"."?+?fileExt;?? ?? ????????try?{???? ????????????InputStream?in?=?new?FileInputStream(file);???? ????????????File?uploadFile?=?new?File(savePath,?newFileName);???? ????????????OutputStream?out?=?new?FileOutputStream(uploadFile);???? ????????????byte[]?buffer?=?new?byte[1024?*?1024];???? ????????????int?length;???? ????????????while?((length?=?in.read(buffer))?>?0)?{???? ????????????????out.write(buffer,?0,?length);???? ????????????}???? ???? ????????????in.close();???? ????????????out.close();???? ????????}?catch?(FileNotFoundException?ex)?{???? ????????????ex.printStackTrace();???? ????????}?catch?(IOException?ex)?{???? ????????????ex.printStackTrace();???? ????????} ? ?
上傳是items一直是空list。導致原因是struts2把原始的原來S2為簡化上傳功能,把所有的enctype="multipart/form-data"表單做了wrapper最后把HttpServletResquest封裝成?org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper?怪不得我的?ServletFileUpload.parseRequest(request)不行!!!?
看我怎么改!廢話不多說,直接貼代碼
轉載于:https://www.cnblogs.com/blueherb/p/3998583.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的转:upload.parseRequest为空的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BZOJ1835: [ZJOI2010]
- 下一篇: BZOJ3864 hero meet d