基于Struts2的文件上传(转)

简介:

1 文件上传的表单

<html>
  <head>
    <base href="<%=basePath%>">
    <title>Struts2文件上传</title>

  </head>
 
  <body>
   <center>
   <h1>Struts 2完成上传</h1>
   <form action="upload.action" method="post" enctype="multipart/form-data">
    <table>
     <tr>
      <td>用户名:</td>
      <td><input type="text" name="username" ></td>
     </tr>
     <tr>
      <td>上传文件:</td>
      <td><input type="file" name="myFile"></td>
     </tr>
     <tr>
      <td><input type="submit" value="上传"></td>
      <td><input type="reset"></td>
     </tr>
    </table>
   </form>
  </center>
  </body>
</html>

 

 

2 文件上传的Action

 

这里需要除了Struts本身需要的包以外,还有

commons-fileupload-1.2.1.jar

commons-io-1.4.jar

这两个包。

 

Action中字段必须遵守一下命名规则

private File xxx;封装该文件对应的文件内容。

private String xxxFileName;该文件的文件名称。

private String xxxContentType;该文件的文件类型。

 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport

{
 // username属性用来封装用户名
 private String username;
 
 // myFile属性用来封装上传的文件
 private File myFile;
 
 // myFileContentType属性用来封装上传文件的类型
 private String myFileContentType;

 // myFileFileName属性用来封装上传文件的文件名
 private String myFileFileName;

 
 //获得username值
 public String getUsername() {
  return username;
 }

 //设置username值
 public void setUsername(String username) {
  this.username = username;
 }

 //获得myFile值
 public File getMyFile() {
  return myFile;
 }

 //设置myFile值
 public void setMyFile(File myFile) {
  this.myFile = myFile;
 }

 //获得myFileContentType值
 public String getMyFileContentType() {
  return myFileContentType;
 }

 //设置myFileContentType值
 public void setMyFileContentType(String myFileContentType) {
  this.myFileContentType = myFileContentType;
 }

 //获得myFileFileName值
 public String getMyFileFileName() {
  return myFileFileName;
 }

 //设置myFileFileName值
 public void setMyFileFileName(String myFileFileName) {
  this.myFileFileName = myFileFileName;
 }

 public String execute() throws Exception {
  
  //基于myFile创建一个文件输入流
  InputStream is = new FileInputStream(myFile);
  
  // 设置上传文件目录(这样设置的目录在tomcat中)
  String uploadPath = ServletActionContext.getServletContext()
    .getRealPath("/upload");
  
  // 设置目标文件
  File toFile = new File(uploadPath, this.getMyFileFileName());
  
  // 创建一个输出流
  OutputStream os = new FileOutputStream(toFile);

  //设置缓存
  byte[] buffer = new byte[1024];

  int length = 0;

  //读取myFile文件输出到toFile文件中
  while ((length = is.read(buffer)) > 0) {
   os.write(buffer, 0, length);
  }
  System.out.println("上传用户"+username);
  System.out.println("上传文件名"+myFileFileName);
  System.out.println("上传文件类型"+myFileContentType);
  //关闭输入流
  is.close();
  //关闭输出流
  os.close();
  return SUCCESS;
 }

}

 

 

3 Struts.xml

<struts>

 <package name="struts2" extends="struts-default">
  <action name="upload" class="net.hncu.struts2.action.UploadAction">

   <interceptor-ref name="fileUpload">
       <param name="allowedTypes"> application/msword</param>
       <param name="maximumSize">20000</param>
   </interceptor-ref>
   <interceptor-ref name="defaultStack"></interceptor-ref>


   <result name="success">/result.jsp</result>
   <result name="input">/upload.jsp</result>
  </action>
 </package>

</struts>

 

关于上传文件大小的控制,请参见:http://blog.sina.com.cn/s/blog_67aaf4440100ya3w.html

 原帖地址:http://ihanfeng.iteye.com/blog/834232


 

目录
相关文章
|
数据可视化 BI
探索ERP系统的移动端应用与移动办公解决方案
探索ERP系统的移动端应用与移动办公解决方案
484 2
|
Shell Linux C语言
【Shell 命令集合 系统设置 】⭐⭐⭐Linux 清除终端屏幕内容 clear命令 使用指南
【Shell 命令集合 系统设置 】⭐⭐⭐Linux 清除终端屏幕内容 clear命令 使用指南
342 0
|
算法 网络架构
router和route的区别?
router和route的区别?
860 0
|
网络协议 网络架构
网络协议与攻击模拟-06-ICMP重定向
网络协议与攻击模拟-06-ICMP重定向
342 0
|
10月前
|
安全 数据安全/隐私保护 数据格式
如何在使用路由参数传递数据时确保数据的安全性?
在使用路由参数传递数据时,要充分考虑数据的安全性问题。通过采取多种措施,如加密、验证、授权、传输安全、脱敏、访问控制、审计和维护等,可以有效提高数据的安全性,保护用户的隐私和系统的安全。同时,要不断加强安全意识,及时关注安全动态,以应对不断变化的安全威胁。
228 58
|
11月前
|
存储 开发框架 开发工具
Electron有哪些使用场景
【10月更文挑战第13天】Electron有哪些使用场景
717 0
|
Ubuntu 安全 网络安全
Ubuntu中关闭防火墙
Ubuntu中关闭防火墙
549 2
|
弹性计算 应用服务中间件
注册阿里云免费领取云服务器,全流程账号注册、实名认证到免费服务器申请
阿里云账号注册与免费服务器申请流程: 1. 访问官网并点击注册,支持手机验证码、支付宝、淘宝和钉钉注册。 2. 手机号注册需获取验证码完成注册;支付宝等方式注册自动关联实名信息。 3. 实名认证:个人推荐支付宝认证,扫码授权即可;企业认证同样便捷。 4. 免费服务器申请:访问免费中心,选择服务器点击“立即试用”,最长期限3个月。
|
数据安全/隐私保护 网络架构
telnet实验操作 wireshark抓包实验操作
telnet实验操作 wireshark抓包实验操作
|
前端开发 小程序
【微信小程序5】利用canvas实现纯色背景抠图功能
【微信小程序5】利用canvas实现纯色背景抠图功能
657 0