文件上傳
multipart/form-data<input type=“file”>字段.<s:form action="testUpload" enctype="multipart/form-data"> <s:textfield name="userName[0]" label="用戶-1"></s:textfield> <s:file name="photos" label="照片"></s:file> <s:textfield name="userName[1]" label="用戶-2"></s:textfield> <s:file name="photos" label="照片"></s:file> <s:textfield name="userName[2]" label="用戶-3"></s:textfield> <s:file name="photos" label="照片"></s:file> <s:submit value="提交"></s:submit></s:form>
public class UploadAction extends ActionSupport{ @Setter@Getter private List<File> photos; @Setter@Getter private List<String> photosContentType; @Setter@Getter private List<String> photosFileName; @Setter@Getter private List<String> userName; public String testUpload() throws IOException { System.out.println("userName: "+userName); System.out.println("photos: "+photos); System.out.println("photosFileName: "+ photosFileName); System.out.println("photosContentType: "+photosContentType); // 將文件傳到服務器根目錄下upload文件下// 獲取ServletContext ServletContext servletContext = ServletActionContext.getServletContext(); //獲取真實路徑String realPath = servletContext.getRealPath("/upload"); System.out.println(realPath); File uploadFile = new File(realPath); //判斷路徑是否存在if (!uploadFile.exists()){ //不存在創建uploadFile.mkdir(); } for (int i = 0; i < photos.size(); i++) { UUID uuid = UUID.randomUUID(); FileUtils.copyFile(photos.get(i), new File(realPath + "/" + uuid + photosFileName.get(i))); } return SUCCESS; }}1.處理幾個小問題?
1.文件名重名,一般可以在文件名的前面生成一個UUID作為前綴。
2.限制單個文件的大小
3.限製文件的類型
4.限制總文件的大小
2.在Struts2 中提供了FileUpload 攔截器可以給我們設置這些屬性值
FileUpload 攔截器有3 個屬性可以設置.
注意:在org.apache.struts2 下的default.properties 中有對上傳的文件總的大小的限制. 可以使用常量的方式來修改該限制struts.multipart.maxSize=2097152
<constant name="struts.devMode" value="true"/> <!-- 在這修改總文件的的大小--> <constant name="struts.multipart.maxSize" value="2097152"/> <package name="default" namespace="/" extends="struts-default"> <interceptors> <interceptor-stack name="myInterceptor"> <interceptor-ref name="defaultStack"> <!-- 修改單個文件大小,Commons FileUpload 組件默認接受上傳文件總的最大值為2M --> <param name="fileUpload.maximumSize">57,408</param> <!-- 允許上傳的文件類型--> <param name="fileUpload.allowedTypes">image/pjpeg,image/gif</param> <!-- 允許上傳文件的擴展名--> <param name="fileUpload.allowedExtensions">jpg,gif</param> </interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="myInterceptor"></default-interceptor-ref> <action name="testUpload" method="testUpload"> <result name="success">/WEB-INF/views/success.jsp</result> <result name="input">/upload.jsp</result> </action></package>
一.上傳文件相關的錯誤消息?
1.與文件上傳有關的出錯消息在struts-messages.properties 文件裡預定義.
2.可以在文件上傳Action 相對應的資源文件或者在i18n_zh_CN.properties 國際化資源文件中重新定義錯誤消息
struts.messages.error.file.too.large=你傳的文件太大了struts.messages.error.content.type.not.allowed=文件類型錯誤struts.messages.error.file.extension.not.allowed=擴展名錯誤struts.messages.upload.error.SizeLimitExceededException=文件總大小超過上限
文件下載
在某些應用程序裡, 可能需要動態地把一個文件發送到用戶的瀏覽器中, 而這個文件的名字和存放位置在編程時是無法預知的
示例代碼
<a href="testDownLoad">下載</a>
public class DownLoadAction extends ActionSupport{ //通常以下這幾個參數會在Action 中提供@Setter@Getter private String contentType; @Setter@Getter private long contentLength; @Setter@Getter private String contentDisposition; @Setter@Getter private InputStream inputStream; public String testDownLoad() throws FileNotFoundException, UnsupportedEncodingException { //獲取ServletContext ServletContext servletContext = ServletActionContext.getServletContext(); //獲取文件的路徑String realPath = servletContext.getRealPath("/WEB-INF/file/至少還有你.mp3"); //獲取文件的流inputStream = new FileInputStream(realPath); //設置文件的類型contentType = servletContext.getMimeType(realPath); //獲取文件的長度contentLength = new File(realPath).length(); //設置文件名String fileName = "至少還有你.mp3"; fileName = new String(fileName.getBytes("gbk"),"iso8859-1"); contentDisposition = "attachment;filename="+fileName; return SUCCESS; }}<!-- 文件下載--><action name="testDownLoad" method="testDownLoad"> <result type="stream"> <!-- 文件緩衝大小--> <param name="bufferSize">2048</param> </result></action>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。