struts2文件上传,下载
生活随笔
收集整理的這篇文章主要介紹了
struts2文件上传,下载
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
目錄
- 1. 頁(yè)面表單
- 2. 上傳下載實(shí)現(xiàn)
1. 頁(yè)面表單
<HTML><HEAD><TITLE>上傳下載圖片</TITLE><meta http-equiv="Content-Type" content="text/html; charset=GBK"></head><body><form enctype="multipart/form-data" action="hello/fileUploadAction!fileUpload" method="post"><table><tr><td>文件:</td><td><input type="file" name="uploadImage"></td></tr><tr><td colspan="2"><input type="submit" value="upload"><input type="reset" value="重 置"></td></tr></table><img alt="qian" src="hello/fileUploadAction!fileDownload"></form></body> </html>2. 上傳下載實(shí)現(xiàn)
package action;import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.UUID;import org.apache.commons.io.FileUtils;import base.BaseAction;public class FileUploadAction extends BaseAction {private static final long serialVersionUID = 1L;// uploadImage 為表單文件名稱(chēng)private File uploadImage;private String uploadImageFileName; // 文件名 private String uploadImageContentType; // 文件類(lèi)型public File getUploadImage() {return uploadImage;}public void setUploadImage(File uploadImage) {this.uploadImage = uploadImage;}public String getUploadImageFileName() {return uploadImageFileName;}public void setUploadImageFileName(String uploadImageFileName) {this.uploadImageFileName = uploadImageFileName;}public String getUploadImageContentType() {return uploadImageContentType;}public void setUploadImageContentType(String uploadImageContentType) {this.uploadImageContentType = uploadImageContentType;}public String fileUpload() {String path = "E:ssh\\struts2\\Struts_06\\fileDir";// 創(chuàng)建存儲(chǔ)文件的文件夾File fileDir = new File(path);if (!fileDir.exists()) {fileDir.mkdirs();}int count = uploadImageFileName.lastIndexOf(".");// 得到文件后綴名String ext = uploadImageFileName.substring(count);UUID uuid = UUID.randomUUID();String fileName = uuid + ext;File file = new File(path + "\\" + fileName);if (!file.exists()) {try {// 創(chuàng)建文件file.createNewFile();} catch (IOException e) {e.printStackTrace();}}try {// 復(fù)制文件到上傳的路徑FileUtils.copyFile(uploadImage, file);} catch (IOException e) {e.printStackTrace();}return "index";}public void fileDownload() {File file = new File("E:ssh\\struts2\\Struts_06\\fileDir\\4fcd0f06-575a-47fc-a010-d0a232ef9947.jpg");System.out.println(file);// 清除空格resp.reset();// 設(shè)置文件 ContentType 自動(dòng)判斷下載類(lèi)型resp.setContentType("multipart/form-data");// 設(shè)置文件頭resp.setHeader("Content-Disposition", "attachment;fileName="+file.getName());InputStream in = null;OutputStream out = null;try {in = new FileInputStream(file);out = resp.getOutputStream();// 準(zhǔn)備字節(jié)數(shù)組裝 byte[] b = new byte[(int)file.length()];// 將文件讀取到內(nèi)存中in.read(b);// 使用response響應(yīng)流 寫(xiě)到頁(yè)面out.write(b);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {if (out != null)out.close();if (in != null)in.close();} catch (IOException e) {e.printStackTrace();}}}public static void main(String[] args) {String str = "fhh.jpg";int i = str.lastIndexOf(".");String string = str.substring(i);System.out.println(string);}}總結(jié)
以上是生活随笔為你收集整理的struts2文件上传,下载的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: android 文件选择器_Androi
- 下一篇: 程序员日报周报月报 模板