Struts2单文件上传

1.引入jar包

我们需要格外引入2和commons的jar


[java]  view plain  copy
  1.   
  2.     commons-fileupload  
  3.     commons-fileupload  
  4.     1.3.1  
  5.   
  6.   
  7.   
  8.     commons-io  
  9.     commons-io  
  10.     1.4  
  11.   


2.Action类:

private String uploadContentType;
    private String uploadFileName;

 

2个属性名  必须要以ContentType     FileName  结尾


[java]  view plain  copy
  1. package cn.happy.struts09file;  
  2.   
  3. import com.opensymphony.xwork2.Action;  
  4. import org.apache.struts2.ServletActionContext;  
  5.   
  6. import java.io.File;  
  7. import java.io.FileInputStream;  
  8. import java.io.FileOutputStream;  
  9.   
  10. /** 
  11.  * Created by linlin on 2017/10/30. 
  12.  */  
  13. public class FileAction implements Action{  
  14.   
  15.     private File upload;  
  16.     private String uploadContentType;  
  17.     private String uploadFileName;  
  18.     private String savePath;  
  19.     public String execute() throws Exception {  
  20.         byte[] buffer=new byte[1024];  
  21.         FileInputStream fis=new FileInputStream(getUpload());  
  22.         FileOutputStream fos=new FileOutputStream(getSavePath()+"\\"+getUploadFileName());  
  23.         int length=fis.read(buffer);  
  24.         while (length>0){  
  25.             fos.write(buffer,0,length);  
  26.             length=fis.read(buffer);  
  27.         }  
  28.         fos.flush();  
  29.         fos.close();  
  30.         fis.close();  
  31.         return SUCCESS;  
  32.     }  
  33.   
  34.     public File getUpload() {  
  35.         return upload;  
  36.     }  
  37.   
  38.     public void setUpload(File upload) {  
  39.         this.upload = upload;  
  40.     }  
  41.   
  42.     public String getUploadContentType() {  
  43.         return uploadContentType;  
  44.     }  
  45.   
  46.     public void setUploadContentType(String uploadContentType) {  
  47.         this.uploadContentType = uploadContentType;  
  48.     }  
  49.   
  50.     public String getUploadFileName() {  
  51.         return uploadFileName;  
  52.     }  
  53.   
  54.     public void setUploadFileName(String uploadFileName) {  
  55.         this.uploadFileName = uploadFileName;  
  56.     }  
  57.   
  58.     public String getSavePath() {  
  59.         return ServletActionContext.getServletContext().getRealPath(savePath);  
  60.     }  
  61.   
  62.     public void setSavePath(String savePath) {  
  63.         this.savePath = savePath;  
  64.     }  
  65. }  



3.struts.xml

[java]  view plain  copy
  1. "1.0" encoding="UTF-8"?>  
  2.         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  3.         "http://struts.apache.org/dtds/struts-2.0.dtd">  
  4.   
  5.   
  6.   
  7.   
  8.     <package name="day-09" namespace="" extends="struts-default">  
  9.   
  10.         "uploadAction" class="cn.happy.struts09file.FileAction">  
  11.           "savePath">/upload  
  12.             "success">/file/strutsupload.jsp  
  13.           
  14.   
  15.     package>  
  16.   
  17.   

4.页面:

[java]  view plain  copy
  1. <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>  
  2. <%@ taglib prefix="s" uri="/struts-tags" %>  
  3.   
  4.   
  5.     Struts2文件上传  
  6.   
  7.   
  8. "uploadAction" enctype="multipart/form-data" method="post">  
  9.     "upload" label="选择文件"/>
      
  10.     "submit" value="文件上传"/>  
  11.   
  12.   
  13.   

2.

[java]  view plain  copy
  1. <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>  
  2. <%@ taglib prefix="s" uri="/struts-tags" %>  
  3.   
  4.   
  5.     Struts2文件上传页面  
  6.   
  7.   
  8. 文件是: "uploadFileName">
      
  9. 文件类型:"uploadContentType">
      
  10.   
  11.   


web.xml

[java]  view plain  copy
  1. "1.0" encoding="UTF-8"?>  
  2. "http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  
  3.   
  4.   
  5.   struts2  
  6. class>  
  7. org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
  8. class>  
  9.           
  10.   
  11. struts2  
  12. /*  
  13.   
  14.   
  15.   
  16.   contextConfigLocation  
  17.   classpath:struts.xml  
  18.   
  19.   
  20.   class>org.springframework.web.context.ContextLoaderListenerclass>  
  21.   
  22.   

你可能感兴趣的:(Struts2单文件上传)