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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

commons-fileupload 图片上传示例

發布時間:2025/6/15 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 commons-fileupload 图片上传示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
該圖片上傳功能包括:?
? ??1.上傳文件類型檢查?
? ??2.上傳文件大小控制?
? ??3.解決中文文件名亂碼?

引入的.jar文件:?
? ?commons-fileupload-1.2.1.jar?
? ?commons-io-1.2.jar?
JSP頁面: function form_submit(){ var img=document.getElementByIdx_x("bg_img").value; if(img==""){ alert("請選擇文件"); return false; } var rex=/(\.jpg|\.gif|\.png)$/i; if(!rex.test(img)){ alert("文件類型錯誤"); return false; } }




后臺處理類:import java.io.File; import java.util.Iterator; import java.util.List; import java.util.Random;import org.apache.commons.fileupload.DiskFileUpload; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadBase;public String upload() throws Exception { String realPath = session.getServletContext().getRealPath("/"); String uploadPath = "/upload/"; String tempPath = "/upload/temp/"; DiskFileUpload fu = new DiskFileUpload();fu.setSizeMax(4194304); // 設置最大文件尺寸,這里是4MB fu.setSizeThreshold(4096); // 設置緩沖區大小,這里是4kb fu.setRepositoryPath(realPath+tempPath); // 設置臨時目錄 fu.setHeaderEncoding("UTF-8");//設置字符集 防止中文文件名亂碼 List fileItems = fu.parseRequest(request); //得到提交的數據 Iterator i = fileItems.iterator();ResJson resJson = new ResJson(false, "LOGO上傳失敗!");String allowExts = ".jpg.jpeg.gif.png"; String newname = getnumber();FileItem imgFile = null;try{ while(i.hasNext()) {FileItem fi = (FileItem)i.next();String fileName = fi.getName();// 獲得文件名,其中包括用戶提交的路徑即html file的value if(fileName != null) {String fileExt = fileName.substring(fileName.lastIndexOf("."));if(allowExts.indexOf(fileExt.toLowerCase())==-1){resJson.setSuccess(false);resJson.setMsg("LOGO上傳失敗!此文件類型不允許上傳,請重新選擇后綴為“.jpg 或 .jpeg 或 .gif 或 .png”的文件后上傳!");}else if(fi.getFieldName().equals("bg_img")){ //多文件的話判斷html file的name imgFile=fi; }}}if(imgFile!=null){String imgFileName = imgFile.getName(); imgFileName = imgFileName.substring(imgFileName.lastIndexOf("\")+1); newname = newname + imgFileName.substring(imgFileName.length() - 4,imgFileName.length());File fImg = new File(realPath + uploadPath + newname);System.out.println("imgFileName="+imgFileName+" fImg="+fImg+" newname="+newname);if(fImg.exists()){resJson.setSuccess(false);resJson.setMsg("LOGO上傳失敗!此文件類型不允許上傳!");}else{ imgFile.write(fImg);resJson.setSuccess(true);resJson.setMsg("LOGO上傳成功!");resJson.setContent("upload/" + newname);}}}catch(FileUploadBase.SizeLimitExceededException e){resJson.setSuccess(false); resJson.setMsg("LOGO上傳失敗!文件大小超出,最大上傳限制4M");}out.append(JSON.toJSONString(resJson)); return NULL; }public String getnumber() { String ; //獲取隨機 Random r = new Random(); String str = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890"; for(int i=0; i<30; i++) { int num = r.nextInt(str.length()); char c = str.charAt(num); autoid = autoid + c; } return autoid; }

總結

以上是生活随笔為你收集整理的commons-fileupload 图片上传示例的全部內容,希望文章能夠幫你解決所遇到的問題。

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