struts上传

一.单文件上传 
添加jar包 
commons-fileupload-1.2.1.jar 
commons-io-1.4.jar 
在webroot创建一个文件如upload用于保存上传的文件 
1.upload.jsp 

Html代码   收藏代码
  1. <body>  
  2.   <s:form action="upload" enctype="multipart/form-data">  
  3.   <s:file name="file" label="file"></s:file>  
  4.   <s:submit label="upload"></s:submit>  
  5.  </s:form>  
  6. </body>  



2.UploadAction.java 

Java代码   收藏代码
  1. package com.test.upload;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.InputStream;  
  7. import java.io.OutputStream;  
  8.   
  9. import org.apache.struts2.ServletActionContext;  
  10.   
  11. import com.opensymphony.xwork2.ActionSupport;  
  12.   
  13. public class UploadAction extends ActionSupport {  
  14.  private File file;  
  15.  private String fileFileName;  
  16.  private String fileContentType;  
  17.  public File getFile() {  
  18.   return file;  
  19.  }  
  20.  public void setFile(File file) {  
  21.   this.file = file;  
  22.  }  
  23.  public String getFileContentType() {  
  24.   return fileContentType;  
  25.  }  
  26.  public void setFileContentType(String fileContentType) {  
  27.   this.fileContentType = fileContentType;  
  28.  }  
  29.  public String getFileFileName() {  
  30.   return fileFileName;  
  31.  }  
  32.  public void setFileFileName(String fileFileName) {  
  33.   this.fileFileName = fileFileName;  
  34.  }  
  35.  @Override  
  36.  public String execute() throws Exception {  
  37.   //1.构建一个输入流  
  38.   InputStream is=new FileInputStream(file);  
  39.   //2.构建一个上传文件路径  
  40.   String root=ServletActionContext.getRequest().getRealPath("/upload");  
  41.   //3.获得一个本地文件  
  42.   File diskFile=new File(root,this.getFileFileName());  
  43.   //4.构建输出流  
  44.   OutputStream os=new FileOutputStream(diskFile);  
  45.   //5.能过字节写入输出流  
  46.   byte[] buffer=new byte[400];  
  47.   int length=0;  
  48.   while((length=is.read(buffer))>0)  
  49.   {  
  50.    os.write(buffer,0,length);  
  51.   }  
  52.   is.close();  
  53.   os.close();  
  54.   return SUCCESS;  
  55.  }  
  56. }  


3.配置struts.xml 

Xml代码   收藏代码
  1. <constant name="struts.multipart.saveDir" value="c:\"></constant>  
  2.  <package name="FileuploadTest" extends="struts-default">  
  3.   <action name="upload" class="com.test.upload.UploadAction">  
  4.    <result name="success">/uploadSuccess.jsp</result>  
  5.   </action>  
  6.  </package>  



二.多文件上传 
1.upload.jsp页面 

Html代码   收藏代码
  1. <body>  
  2.   <s:form action="upload" enctype="multipart/form-data">  
  3.    <s:file name="file" label="file"></s:file>  
  4.    <s:file name="file" label="file1"></s:file>  
  5.    <s:file name="file" label="file2"></s:file>  
  6.    <s:submit label="submit"></s:submit>  
  7.   </s:form>  
  8.   </body>  


2.UploadAction.java 

Java代码   收藏代码
  1. package com.test.upload;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.InputStream;  
  7. import java.io.OutputStream;  
  8. import java.util.List;  
  9.   
  10. import org.apache.struts2.ServletActionContext;  
  11.   
  12. import com.opensymphony.xwork2.ActionSupport;  
  13.   
  14. public class UploadAction extends ActionSupport {  
  15.  private List<File> file;  
  16.   
  17.  private List<String> fileFileName;  
  18.   
  19.  private List<String> fileContentType;  
  20.   
  21.  public List<File> getFile() {  
  22.   return file;  
  23.  }  
  24.   
  25.  public void setFile(List<File> file) {  
  26.   this.file = file;  
  27.  }  
  28.   
  29.  public List<String> getFileContentType() {  
  30.   return fileContentType;  
  31.  }  
  32.   
  33.  public void setFileContentType(List<String> fileContentType) {  
  34.   this.fileContentType = fileContentType;  
  35.  }  
  36.   
  37.  public List<String> getFileFileName() {  
  38.   return fileFileName;  
  39.  }  
  40.   
  41.  public void setFileFileName(List<String> fileFileName) {  
  42.   this.fileFileName = fileFileName;  
  43.  }  
  44.   
  45.  @Override  
  46.  public String execute() throws Exception {  
  47.   for (int i = 0; i < file.size(); i++) {  
  48.    // 1.构建一个输入流  
  49.    InputStream is = new FileInputStream(file.get(i));  
  50.    // 2.构建一个上传文件路径  
  51.    String root = ServletActionContext.getRequest().getRealPath(  
  52.      "/upload");  
  53.    // 3.获得一个本地文件  
  54.    File diskFile = new File(root, this.getFileFileName().get(i));  
  55.    // 4.构建输出流  
  56.    OutputStream os = new FileOutputStream(diskFile);  
  57.    // 5.能过字节写入输出流  
  58.    byte[] buffer = new byte[400];  
  59.    int length = 0;  
  60.    while ((length = is.read(buffer)) > 0) {  
  61.     os.write(buffer, 0, length);  
  62.    }  
  63.    is.close();  
  64.    os.close();  
  65.   }  
  66.   return SUCCESS;  
  67.  }  
  68. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值