struts2實現檔案上傳下載

來源:互聯網
上載者:User

標籤: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實現檔案上傳下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.