標籤:ati on() att adf cte 檔案 roo nal contex
代碼比較粗糙,這裡只做個記錄,方便日後查詢
/** * 上傳檔案代碼 */public class UploadAndDownloadAction extends ActionSupport{ // 擷取檔案 private File myFile; // 擷取檔案的名稱 private String myFileFileName; // 擷取檔案的類型 private String myFileContentType; public File getMyFile() { return myFile; } public void setMyFile(File myFile) { this.myFile = myFile; } public String getMyFileFileName() { return myFileFileName; } public void setMyFileFileName(String myFileFileName) { this.myFileFileName = myFileFileName; } public String getMyFileContentType() { return myFileContentType; } public void setMyFileContentType(String myFileContentType) { this.myFileContentType = myFileContentType; } public boolean upload() { // 1.確定上傳的檔案儲存的位置 String uploadRootPath = "/common/download/template"; String dir = Struts2Util.getRequest().getRealPath(uploadRootPath); // 2.判斷這個檔案夾是否存在,不存在建立 File file = new File(dir); if (!file.exists()) { file.mkdir(); } // 3.將檔案放到InputStream裡面,然後outputstream寫入檔案 InputStream is = null; OutputStream os = null; try { is = new BufferedInputStream(new FileInputStream(myFile)); os = new BufferedOutputStream(new FileOutputStream(dir + "\\" + myFileFileName)); byte[] buffer = new byte[1024]; int byteLen = 0; while ((byteLen = is.read(buffer)) > 0) { os.write(buffer, 0, byteLen); } domain.setTemplateName(myFileFileName); domain.setTemplateUrl(uploadRootPath+ "\\" + myFileFileName); domain.setTemplateFullPath(dir+ "\\" + myFileFileName); } catch (Exception e) { } finally { if (is != null) { try { is.close(); } catch (IOException e) { } } if (os != null) { try { os.close(); } catch (IOException e) { } } } return true; }}
/** * 檔案下載 */public class UploadAndDownloadAction extends ActionSupport { private String fileName; public String getFileName() { try{ fileName = URLEncoder.encode(fileName,"UTF-8"); }catch(Exception e){ throw new RuntimeException(); } return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } //2.3:返迴流的方法 下載 public InputStream getAttrInputStream() throws ItsException, UnsupportedEncodingException{ String fileName=Struts2Util.getRequest().getParameter("fileName"); this.setFileName(fileName); InputStream is=ServletActionContext.getServletContext().getResourceAsStream("路徑"); return is; }else{ return null; } } public String getDownloadFile() throws ItsException { return "success"; } /** * struts2 配置 */ <action name="uploadAndDownload*" class="uploadAndDownloadAction" method="{1}"> <result name="uploadTemplate">/common/download/jsp/uploadTemplate.jsp</result> <!-- 下載操作 --> <result name="success" type="stream"> <param name="contentType">application/octet-stream</param> <param name="inputName">attrInputStream</param> <param name="contentDisposition">attachment;filename="${fileName}"</param> <param name="bufferSize">1024</param> </result> </action>}
/** * 刪除檔案 */public static boolean delete(String path) { File file = new File(path); if (!file.exists()) { return false; }else{ return deleteFile(path); }}/** * 刪除檔案 */public static boolean deleteFile(String path) { File file = new File(path); // 如果檔案路徑所對應的檔案存在,並且是一個檔案,則直接刪除 if (file.delete()) { return true; }else { return false; }}
struts2實現檔案上傳下載