文件上传struts2 实现文件上传功能(3)

(三)使用struts2进行文件上传、下载 
需引入两个jar包

commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar

这两个jar包在struts2.1.6版本中已经自带,较低版本需到apache网站下载,网址:http://commons.apache.org/

1、单文件上传
upload3.jsp

 1 <% @ page language="java" contentType="text/html; charset=GB18030"
 2    pageEncoding="GB18030"
%>
 3 <% @ taglib prefix="s" uri="/struts-tags" %>
 4 <! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
 5 < html >
 6      < head >
 7          < meta  http-equiv ="Content-Type"  content ="text/html; charset=GB18030" >
 8          < title > Insert title here </ title >
 9      </ head >
10      < body >
11          < s:form  action ="upload"  method ="post"  theme ="simple"
12             enctype ="multipart/form-data" >
13              < table  align ="center"  width ="50%"  border ="1" >
14                  < tr >
15                      < td >
16                         username
17                      </ td >
18                      < td >
19                          < s:textfield  name ="username" ></ s:textfield >
20                      </ td >
21                  </ tr >
22                  < tr >
23                      < td >
24                         password
25                      </ td >
26                      < td >
27                          < s:password  name ="password" ></ s:password >
28                      </ td >
29                  </ tr >
30                  < tr >
31                      < td >
32                         file
33                      </ td >
34
35                      < td >
36                          < s:file  name ="file" ></ s:file >
37                         
38                      </ td >
39                  </ tr >
40                  < tr >
41                      < td >
42                          < s:submit  value =" submit " ></ s:submit >
43                      </ td >
44                      < td >
45                          < s:reset  value =" reset " ></ s:reset >
46                      </ td >
47                  </ tr >
48              </ table >
49          </ s:form >
50      </ body >
51 </ html >

web.xml中的配置

     < filter >
        
< filter-name > struts2 </ filter-name >
        
< filter-class >
            org.apache.struts2.dispatcher.FilterDispatcher
        
</ filter-class >
    
</ filter >

    
< filter-mapping >
        
< filter-name > struts2 </ filter-name >
        
< url-pattern > /* </ url-pattern >
    
</ filter-mapping >


struts.xml中的配置

 1 <? xml version="1.0" encoding="GBK"  ?>
 2 <! DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 4     "http://struts.apache.org/dtds/struts-2.0.dtd" >
 5     
 6 < struts >
 7      < constant  name ="struts.i18n.encoding"  value ="gbk" ></ constant >
 8      < constant  name ="struts.multipart.saveDir"  value ="c:\" ></ constant >
 9      < package  name ="struts2"  extends ="struts-default" >
10          < action  name ="upload"  class ="com.test.action.UploadAction" >
11              < result  name ="success" > /upload/result3.jsp </ result >
12          </ action >
13      </ package >
14 </ struts >

UploadAction.java

 1 package  com.test.action;
 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 @SuppressWarnings( " serial " )
14 public   class  UploadAction  extends  ActionSupport  {
15    private String username;
16    private String password;
17    private File file;
18    private String fileFileName;
19    private String fileContentType;
20
21    public String getUsername() {
22        return username;
23    }

24
25    public void setUsername(String username) {
26        this.username = username;
27    }

28
29    public String getPassword() {
30        return password;
31    }

32
33    public void setPassword(String password) {
34        this.password = password;
35    }

36
37    public File getFile() {
38        return file;
39    }

40
41    public void setFile(File file) {
42        this.file = file;
43    }

44
45    public String getFileFileName() {
46        return fileFileName;
47    }

48
49    public void setFileFileName(String fileFileName) {
50        this.fileFileName = fileFileName;
51    }

52
53    public String getFileContentType() {
54        return fileContentType;
55    }

56
57    public void setFileContentType(String fileContentType) {
58        this.fileContentType = fileContentType;
59    }

60
61    @SuppressWarnings("deprecation")
62    @Override
63    public String execute() throws Exception {
64        InputStream is = new FileInputStream(file);
65        String root = ServletActionContext.getRequest().getRealPath("/upload");
66        File destFile = new File(root, this.getFileFileName());
67        OutputStream os = new FileOutputStream(destFile);
68        byte[] buffer = new byte[400];
69
70        int length = 0;
71
72        while ((length = is.read(buffer)) > 0{
73            os.write(buffer, 0, length);
74        }

75        is.close();
76        os.close();
77        return SUCCESS;
78    }

79}


结果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值