jsp图片上传(commons-fileupload组件)
1.開發環境:
?1)eclipse3.2+tomcat5.5;
?2)創建dynamic web project;
?3)下載:
Commons FileUpload 可以在http://jakarta.apache.org/commons/fileupload/下載
附加的Commons IO? 可以在http://jakarta.apache.org/commons/io/下載
將commons-fileupload-1.2.1.jar commons-io-1.4.jar拷貝到WebContent/WEB-INF//lib目錄;
?
?
2.前臺:
<form method="post" enctype="multipart/form-data" action="upload.jsp" target="_blank">
<%-- 類型enctype用multipart/form-data,這樣可以把文件中的數據作為流式數據上傳,不管是什么文件類型,均可上傳--%>
<table>
<tr><td>作品:<input type="file" name="upfile" size="50"></td></tr>
<tr><td>作者:<input type="text" name="author" size="22">
??????? 標題:<input type="text" name="title" size="22">
??????? <input type="submit" name="submit" value="上 傳"></td></tr>
<tr><td>備注:上傳的jpg圖片(顯示擴展名為.jpg)大小不能超過4M!</td></tr>
</table>
</form>
3.后臺:
1)引用:
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.*"%>
2)代碼:
String id=null;//上傳記錄id
?String destinationfileName=null;//目標文件名
?String author=null;
?String title=null;
?int flag=0;//上傳標志
??? String uploadPath =request.getSession().getServletContext().getRealPath("/")+"upload/";//圖片上傳路徑??
??? String tempPath = request.getSession().getServletContext().getRealPath("/")+"upload/temp/";//圖片臨時上傳路徑???
??? String imagePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/"; //?? 圖片網絡相對路徑?
??? if(!new File(uploadPath).isDirectory()) new File(uploadPath).mkdirs();//?? 文件夾不存在就自動創建:
??? if(!new File(tempPath).isDirectory())new File(tempPath).mkdirs();?
?? ?try {
?? ??DiskFileUpload fu = new DiskFileUpload();?
???? fu.setSizeMax(4194304);//?? 設置最大文件尺寸,這里是4MB? ?
????? fu.setSizeThreshold(4096);//?? 設置緩沖區大小,這里是4kb?
???? fu.setRepositoryPath(tempPath);//?? 設置臨時目錄:?
????? List fileItems = fu.parseRequest(request);//?? 得到所有的文件:
??????? Iterator i = fileItems.iterator();??
???? while(i.hasNext()) {//?? 依次處理表單中每個域
??????????? FileItem file = (FileItem)i.next();//?? 獲得用戶上傳時文件名?
???if (file.isFormField()){? //獲得文本域表單數據??????
????if("author".equals(file.getFieldName())) author=codeToString(file.getString());
????if("title".equals(file.getFieldName())) title=codeToString(file.getString());
????continue;//非file域不處理
???????? ?}???
???String sourcefileName = file.getName();
????? if( sourcefileName.endsWith(".jpg")){//生成上傳后的文件名????
????? Random rd = new Random();
????? Calendar time = Calendar.getInstance();
????? id=String.valueOf(time.get(Calendar.YEAR))
? ???????? + String.valueOf(time.get(Calendar.MONTH)+1)
??????????? + String.valueOf(time.get(Calendar.DAY_OF_MONTH))
??????????? + String.valueOf(time.get(Calendar.HOUR_OF_DAY))
??????????? + String.valueOf(time.get(Calendar.MINUTE))
???????????????????????????????? + String.valueOf(time.get(Calendar.SECOND))
???????????????????????????????? + String.valueOf(rd.nextInt(100));
????? destinationfileName=id+".jpg";
?????????????? File fTmp=new File(uploadPath+destinationfileName);
?????????????? file.write(fTmp);
?????????????? //out.print("<img src="+imagePath+"upload/"+destinationfileName+">");
????? flag=1;//上傳成功標志1
?????????? }else{out.print("上傳失敗,只能上傳jpg文件!"); }
?????? }
?????? //out.print("<script>location.href=/"demo.jsp/";</script>");
??? }catch (IOException e) {out.print("上傳失敗!");}
??? out.flush();
??? out.close();
?
4.主要解決點:
1)問題:form設置enctype="multipart/form-data",request.getParameters函數無法獲取表單域值;
??? 2)解決方案:
if (file.isFormField()){? //獲得文本域表單數據??????
???????? if("author".equals(file.getFieldName())) author=codeToString(file.getString());
????? ??if("title".equals(file.getFieldName())) title=codeToString(file.getString());
????continue;//非file域不處理
??????? }??
總結
以上是生活随笔為你收集整理的jsp图片上传(commons-fileupload组件)的全部內容,希望文章能夠幫你解決所遇到的問題。