《赴日java必備》由會員分享,可在線閱讀,更多相關(guān)《赴日java必備(24頁珍藏版)》請在裝配圖網(wǎng)上搜索。
1、單擊此處編輯母版標題樣式,單擊此處編輯母版文本樣式,第二級,第三級,第四級,第五級,*,Struts2,文件上傳,Struts2,上傳介紹,實現(xiàn)上傳功能,上傳錯誤處理,限制大小,課程安排,多文件上傳,Struts2,文件上傳介紹,Struts2,是通過,Apache,Commons FileUpload,進行文件上傳,Commons FileUpload,通過將,HTTP,數(shù)據(jù)保存到臨時文件夾,Struts,使用,fileUpload,攔截器,將文件綁定到,Action,中,依賴,jar,包,除了,Struts2,基本,jar,包外,還需要:,commons-fileupload-1.2.1.
2、jar,commons-io-1.3.2.jar,FileUpload.jsp,Struts2 File Upload,*,enctype,=“,multipart/form-data,”,設(shè)置為多部分表單數(shù)據(jù)!,FileUploadAction.java,public class FileUploadAction extends ActionSupport ,private static final int,BUFFER_SIZE,= 16 * 1024;,private File myFile;,private String contentType;,private String file
3、Name;,private String imageFileName;,private String caption;,public void,setMyFileContentType,(String contentType) ,this.contentType = contentType;,public void,setMyFileFileName,(String fileName) ,this.fileName = fileName;,public void,setMyFile,(File myFile) ,this.myFile = myFile;,public String getIm
4、ageFileName() ,return imageFileName;,public String getCaption() ,return caption;,public void setCaption(String caption) ,this.caption = caption;,FileUploadAction.java,private static void,copy,(File src, File dst),InputStream in = null;,OutputStream out = null;,in = new,BufferedInputStream,(new,FileI
5、nputStream,(src),BUFFER_SIZE,);,out=new BufferedOutputStream(new,FileOutputStream(dst),BUFFER_SIZE,);,byte buffer = new byte,BUFFER_SIZE,;,while (in.read(buffer) 0) ,out.write(buffer);,*,此方法省略了異常處理,FileUploadAction.java,/,獲取擴展名,private static String,getExtention,(String fileName) ,int pos = fileName
6、.lastIndexOf(.);,return fileName.substring(pos);,/action,的,execute(),方法,public String,execute,() ,imageFileName = new Date().getTime() +,getExtention,(fileName);,File imageFile = new,File(ServletActionContext.,getServletContext,(),.getRealPath(/UploadImages)+ “/” + imageFileName);,copy,(myFile, imag
7、eFile);,return,SUCCESS,;,ShowUpload.jsp,/,struts.xml, ,/ShowUpload.jsp, ,web.xml,struts-cleanup,org.apache.struts2.dispatcher.,ActionContextCleanUp,struts-cleanup,/*,web.xml,struts2,org.apache.struts2.dispatcher.FilterDispatcher,struts2,/*,部署運行測試,調(diào)試運行,錯誤處理,1,1,. 修改struts.xml中的fileUpload action配置加入:,
8、 -, image/bmp,image/png,image/gif,image/pjpeg,image/jpeg,image/jpg,2,0,000,/FileUpload.jsp,錯誤處理,2,2,. 修改消息資源文件,messge.properties,在消息資源文件中加入:,#,不支持的文件類型對應(yīng)的出錯消息,struts.messages.error.content.type.not.allowed=not a image,#文件大小限制出錯消息!,struts.messages.error.file.too.large=Too large!,多文件上傳,將多個,綁定,Action,的
9、數(shù)組或列表。,綁定到列表,1,public class,MultipartFileUploadAction,extends,ActionSupport,private static final,int,BUFFER_SIZE = 16 * 1024;,private String,imageFileName,;,/uploads,臨時存儲上傳的文件!,private List uploads = new,ArrayList,();,/,uploadFileNames,存儲臨時文件的文件名!,private List,uploadFileNames,= new,ArrayList,();,/,u
10、ploadContentTypes,存儲支持的文件類型,!,private List,uploadContentTypes,= new,ArrayList,();,/,afterUploadImageNames,存儲上傳后所有的文件名字,List,afterUploadImageNames,=new,ArrayList,();,綁定到列表,2,public List,getUpload,() ,return,this.uploads,;,public void,setUpload(List, uploads) ,this.uploads,= uploads;,public List,getUp
11、loadFileName,() ,return,this.uploadFileNames,;,public void,setUploadFileName(List,uploadFileNames,) ,this.uploadFileNames,=,uploadFileNames,;,public List,getUploadContentType,() ,return,this.uploadContentTypes,;,public void,setUploadContentType(List,contentTypes,) ,this.uploadContentTypes,=,contentT
12、ypes,;,public List,getAfterUploadImageNames,() ,return,afterUploadImageNames,;,public void,setAfterUploadImageNames(List,afterUploadImageNames,) ,this.afterUploadImageNames,=,afterUploadImageNames,;,綁定到列表,3,private static void,copy(File,src, File,dst,) ,try ,InputStream,in = null;,OutputStream,out =
13、 null;,try ,in = new,BufferedInputStream(new,FileInputStream(src,),BUFFER_SIZE);,out = new,BufferedOutputStream(new,FileOutputStream(dst,),BUFFER_SIZE);,byte buffer = new,byteBUFFER_SIZE,;,while (,in.read(buffer,) 0) ,out.write(buffer,);, finally ,if (null != in) ,in.close,();,if (null != out) ,out.
14、close,();, catch (Exception e) ,e.printStackTrace,();,綁定到列表,4,private static String,getExtention(String,fileName,) ,int,pos =,fileName.lastIndexOf,(.);,return,fileName.substring(pos,);,public String execute() ,for(int,i=0;i,uploads.size();i,+),imageFileName,= new,Date().getTime,() +,getExtention(upl
15、oadFileNames.get(i,);,/,UploadImages,為改名字后圖片放置的目錄,File,imageFile,= new,File(ServletActionContext.getServletContext,(),.,getRealPath(/UploadImages,),+ / +,imageFileName,);,copy(uploads.get(i,),imageFile,);,afterUploadImageNames.add(imageFileName,);,/,測試輸出上傳后生成的圖片名字!,for(int,i=0;i,afterUploadImageName
16、s.size();i,+),System.out.println(afterUploadImageNames.get(i,);,return SUCCESS;,struts.xml,image/,bmp,image/png,image/gif,image/pjpeg,image/jpeg,image/jpg,28000,/,MultipartFileUpload.jsp,/,ShowUpload.jsp,ShowUpload.jsp,img,src,=,UploadImages,/,*,afterUploadImageNames,綁定到,action,的屬性!,Demo,部署運行測試,調(diào)試運行,課程總結(jié),Struts2,上傳介紹,實現(xiàn)上傳功能,上傳錯誤處理,限制大小,多文件上傳,