ID:42677042
大小:32.52 KB
頁數(shù):3頁
時間:2019-09-20
3、FileInputStream;importjava.io.FileOutputStream;importjava.io.InputStream;importjava.io.OutputStream;importjava.net.URL;importjava.util.Date;importorg.apache.struts2.ServletActionContext;importcom.opensymphony.xwork2.ActionSupport;@SuppressWarnings("unused")publicclassFileUploa
4、dActionextendsActionSupport{privatestaticfinallongserialVersionUID=572146812454l;privatestaticfinalintBUFFER_SIZE=16*1024;privateFilemyFile;privateStringcontentType;privateStringfileName;privateStringimageFileName;privateStringcaption;//省略了get和set方法……privatestaticvoidcopy(File
5、src,Filedst){try{InputStreamin=null;OutputStreamout=null;try{in=newBufferedInputStream(newFileInputStream(src),BUFFER_SIZE);out=newBufferedOutputStream(newFileOutputStream(dst),BUFFER_SIZE);byte[]buffer=newbyte[BUFFER_SIZE];while(in.read(buffer)>0){out.write(buffer);}}finally{
6、if(null!=in){in.close();}}if(null!=out){out.close();}}catch(Exceptione){e.printStackTrace();}}privatestaticStringgetExtention(StringfileName){intpos=fileName.lastIndexOf(".");returnfileName.substring(pos);}@OverridepublicStringexecute(){imageFileName=newDate().getTime()+getExt
7、ention(fileName);FileimageFile=newFile(ServletActionContext.getServletContext().getRealPath("/UploadImages")+"/"+imageFileName);copy(myFile,imageFile);returnSUCCESS;}}注意:要在WebRoot下創(chuàng)建一個文件夾UploadImages用于存放圖片,但上傳的圖片只在服務(wù)器上能看到,頁面前端也可以顯示出來Struts.xml的配置 8、dDemo"extends="struts-default">
此文檔下載收益歸作者所有