赴日java必備

上傳人:fgh****35 文檔編號:248218572 上傳時間:2024-10-22 格式:PPT 頁數(shù):24 大?。?55.50KB
收藏 版權(quán)申訴 舉報 下載
赴日java必備_第1頁
第1頁 / 共24頁
赴日java必備_第2頁
第2頁 / 共24頁
赴日java必備_第3頁
第3頁 / 共24頁

下載文檔到電腦,查找使用更方便

15 積分

下載資源

還剩頁未讀,繼續(xù)閱讀

資源描述:

《赴日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)上傳功能,上傳錯誤處理,限制大小,多文件上傳,

展開閱讀全文
溫馨提示:
1: 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
2: 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
3.本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
5. 裝配圖網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

相關(guān)資源

更多
正為您匹配相似的精品文檔

相關(guān)搜索

關(guān)于我們 - 網(wǎng)站聲明 - 網(wǎng)站地圖 - 資源地圖 - 友情鏈接 - 網(wǎng)站客服 - 聯(lián)系我們

copyright@ 2023-2025  zhuangpeitu.com 裝配圖網(wǎng)版權(quán)所有   聯(lián)系電話:18123376007

備案號:ICP2024067431-1 川公網(wǎng)安備51140202000466號


本站為文檔C2C交易模式,即用戶上傳的文檔直接被用戶下載,本站只是中間服務(wù)平臺,本站所有文檔下載所得的收益歸上傳人(含作者)所有。裝配圖網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對上載內(nèi)容本身不做任何修改或編輯。若文檔所含內(nèi)容侵犯了您的版權(quán)或隱私,請立即通知裝配圖網(wǎng),我們立即給予刪除!