日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

转:upload.parseRequest为空

發布時間:2023/12/18 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 转:upload.parseRequest为空 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); upload.setHeaderEncoding("UTF-8"); List items = upload.parseRequest(request); ?

上傳是items一直是空list。導致原因是struts2把原始的原來S2為簡化上傳功能,把所有的enctype="multipart/form-data"表單做了wrapper最后把HttpServletResquest封裝成?org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper?怪不得我的?ServletFileUpload.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();????
  • ????????} ? ?
  • 轉載于:https://www.cnblogs.com/blueherb/p/3998583.html

    創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

    總結

    以上是生活随笔為你收集整理的转:upload.parseRequest为空的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。