commons-fileupload-1.2.2.jarjava
commons-io-2.0.1.jarapp
<!-- 設置全局上傳文件的大小 --> <constant name="struts.multipart.maxSize" value="90000000"/> <!-- 國際化 --> <constant name="struts.custom.i18n.resources" value="/i18n/messages" /> <!-- 定義系統設置模塊 --> <package name="biz" extends="base" namespace="/biz"> <!--系統設置 --> <action name="*_*" class="com.mrp.action.{1}Action" method="{2}"> <result name="success">${forwardView}</result> <result name="error">${forwardView}</result> <result name="input">/error/error.jsp</result> <!-- 限制文件上傳大小和類型 --> <interceptor-ref name="fileUpload"> <param name="maximumSize">50000000</param> <!-- <param name="allowedTypes"> application/msword,application/pdf,application/zip </param> --> </interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> <!-- 文件的下載 --> <result name="download" type="stream"> <!-- 文件類型 —— application/octet-stream 表示無限制 --> <param name="contentType">application/octet-stream;charset=ISO8859-1</param> <!-- 流對象名 —— 去找Action中的getInputStream方法 --> <param name="inputName">inputStream</param> <!-- 文件名 —— 將調用該Action中的getDownloadFileName方法 --> <param name="contentDisposition">attachment;fileName="${downloadFileName}"</param> <!-- <param name="bufferSize">4096</param> --> </result> </action> </package>
/** * 涉及文件的上傳下載的變量 */ private File photo; private String photoFileName; private String photoContentType; private String savePath; private String fileName;//要下載的文件名 private InputStream inputStream; /** * 添加課程材料(包括上傳文件的功能) * @return * @throws Exception * @author uug * @date 2017年12月4日 */ public String add() throws Exception{ System.out.println("**********************upload*********************"); //判斷目錄是否存在,不存在的話要新建 File file = new File(getSavePath()); if(!file.exists() && !file.isDirectory()){ System.out.println(savePath+"目錄不存在,須要重建"); file.mkdir(); } //建立目標文件對象,文件名PhotoFileName,格式_FileName File destFile=new File(getSavePath(),getPhotoFileName()); //把上傳的文件,拷貝到目標文件中 FileUtils.copyFile(getPhoto(), destFile); System.err.println("文件名稱"+getPhotoFileName()); //將前臺轉來的數據賦值給新的對象 tSbject.set新的值進去保存會出錯 TSubject tSubject1 = new TSubject(tSubject.getKeNum(),tSubject.getKeName(), tSubject.getSubject(), tSubject.getSubjectDec(), getPhotoFileName(), tSubject.getTeacher()); subjectService.add(tSubject1); tSubject1.setKeNum(null); tSubject1.setSubject(null); return list2(tSubject.getTeacher()); } /** * 文件的下載 * @return * @throws Exception * @author uug * @date 2017年12月20日 */ public String download(){ System.out.println("********************download****************"); System.out.println("getFileName="+getFileName()); inputStream = ServletActionContext.getServletContext().getResourceAsStream("/upload/" + getFileName()); System.out.println("inputStream="+inputStream); return "download"; } public File getPhoto() { return photo; } public void setPhoto(File photo) { this.photo = photo; } public String getPhotoFileName() { return photoFileName; } public void setPhotoFileName(String photoFileName) { this.photoFileName = photoFileName; } public String getPhotoContentType() { return photoContentType; } public void setPhotoContentType(String photoContentType) { this.photoContentType = photoContentType; } public String getSavePath() { return ServletActionContext.getServletContext().getRealPath("/upload"); } public void setSavePath(String savePath) { this.savePath = savePath; } //爲解決文件名字亂碼。struts2.xml中讀取的是downloadFileName而不是fileName public String getDownloadFileName() throws UnsupportedEncodingException { String downloadFileName = new String(fileName.getBytes(), "ISO8859-1"); return downloadFileName; } public String getFileName() { return fileName; } public void setFileName(String fileName) throws UnsupportedEncodingException { //this.fileName = fileName; this.fileName = new String(fileName.getBytes("ISO-8859-1"),"UTF-8"); } public InputStream getInputStream() { return inputStream; } public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; }
上傳:jsp
<li><label>附件</label><input type="file" name="photo" id="photo"/></li>
下載:this
<a href="<%= basePath%>/biz/Subject_download.action?fileName=${attachment}" class="tablelink">下載</a>
struts.messages.error.file.too.large=\u4E0A\u4F20\u6587\u4EF6\u592A\u5927\uFF0C\u8BF7\u91CD\u65B0\u4E0A\u4F20\uFF01 struts.messages.error.content.type.not.allowed=\u4E0A\u4F20\u6587\u4EF6\u7C7B\u578B\u4E0D\u6B63\u786E\uFF0C\u4E0A\u4F20\u5931\u8D25\uFF01