標籤:位元組 tar text default tco 類型 exist ini let
檔案上傳
1 import java.io.File; 2 3 import org.apache.struts2.ServletActionContext; 4 5 import com.opensymphony.xwork2.ActionSupport; 6 7 public class Upload2Action extends ActionSupport { 8 9 private String username;10 private File[] photo;11 12 private String[] photoFileName;// 用來儲存上傳的檔案名稱,命名規範:表單屬性名稱+FileName13 private String[] photoContentType;// 上傳檔案的MIME類型 jpg gif image/pjpeg14 // image/gif15 16 public String getUsername() {17 return username;18 }19 20 public void setUsername(String username) {21 this.username = username;22 }23 24 public File[] getPhoto() {25 return photo;26 }27 28 public void setPhoto(File[] photo) {29 this.photo = photo;30 }31 32 public String[] getPhotoFileName() {33 return photoFileName;34 }35 36 public void setPhotoFileName(String[] photoFileName) {37 this.photoFileName = photoFileName;38 }39 40 public String[] getPhotoContentType() {41 return photoContentType;42 }43 44 public void setPhotoContentType(String[] photoContentType) {45 this.photoContentType = photoContentType;46 }47 48 public String upload() {49 50 // 建立伺服器端隱藏檔的位置51 String path = ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload");52 File dir = new File(path);53 if (!dir.exists()) {54 dir.mkdirs();55 }56 57 // 把臨時檔案剪下到upload下58 for (int i = 0; i < photo.length; i++) {59 photo[i].renameTo(new File(dir, photoFileName[i]));60 61 }62 63 return "success";64 }65 66 }View Code
xml配置
<action name="upload" class="com.web.action.UploadAction" method="upload"> <!-- 設定上傳的檔案的副檔名類型 <interceptor-ref name="defaultStack"> <param name="fileUpload.allowedExtensions">jpg,gif,png</param> </interceptor-ref> --> <!-- 設定上傳檔案的MIME類型 --> <interceptor-ref name="defaultStack"> <param name="fileUpload.allowedTypes">image/jpeg,image/gif,image/png</param> </interceptor-ref> <result name="success">/success.jsp</result> <result name="input">/uplode.jsp</result> </action>
View Code
檔案下載
1 import java.io.FileInputStream; 2 import java.io.FileNotFoundException; 3 import java.io.InputStream; 4 5 import org.apache.struts2.ServletActionContext; 6 7 import com.opensymphony.xwork2.ActionSupport; 8 9 //實現檔案下載10 public class DownLoadAcion extends ActionSupport {11 12 private InputStream inputStream;// 注意:該成員變數名不能叫in13 private String filename;14 15 public String getFilename() {16 return filename;17 }18 19 public void setFilename(String filename) {20 this.filename = filename;21 }22 23 public InputStream getInputStream() {24 return inputStream;25 }26 27 public void setInputStream(InputStream inputStream) {28 this.inputStream = inputStream;29 }30 31 public String download() throws FileNotFoundException {32 33 // 設定被下載的檔案的路徑34 String path = ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload/struts-default.xml");35 36 // 建立讀取被下載檔案的位元組讀取流37 inputStream = new FileInputStream(path);38 39 filename = "圖片.jpg";40 41 // 接下來讀取檔案並寫入到用戶端不需要自己實現,都被結果類型stream實現了42 43 return "success";44 }45 46 }View Code
xml配置
1 <action name="download" class="com.web.action.DownLoadAcion" method="download">2 <result type="stream">3 <param name="inputName">inputStream</param>4 <param name="contentType">application/octet-stream</param>5 <!-- 通知瀏覽器去下載 -->6 <param name="contentDisposition">attachment;filename=${@[email protected](filename,"UTF-8")}</param>7 </result>8 </action>View Code
struts2檔案上傳/檔案下載