struts2 上传文件

 

struts2  可以很简单的实现文件上传,具体内容如下:

 strut.xml---strut2  配置文件

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 "
http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
 <constant name="struts.custom.i18n.resources" value="message"></constant>
   <constant name="struts.multipart.saveDir" value="D:/"></constant>
 <constant name="struts.multipart.maxSize" value="60240000"></constant>
 <package name="login23" extends="struts-default" namespace="/test">
     
   <action name="upload2" class="com.test.action.UploadAction" >
   <result name="success" type="dispatcher">/uploadresult.jsp</result>
   <result name="input" type="dispatcher">/uploadlist2.jsp</result>
   <interceptor-ref name="fileUpload">
    <param name="allowedTypes">text/html</param>
    <param name="maximumSize">409600</param>
   </interceptor-ref>
   <interceptor-ref name="defaultStack"></interceptor-ref>
  </action>
 </package>
</struts>

 

message_zh_CN.properties---struts2资源文件

 struts.messages.error.content.type.not.allowed=/u4E0A/u4F20/u6587/u4EF6/u7C7B/u578B/u4E0D/u5141/u8BB8/uFF0C/u8BF7/u91CD/u8BD5 /!

//配置上传文件类型不对的错误信息
struts.messages.error.file.too.large=/u4E0A/u4F20/u6587/u4EF6/u8FC7/u5927/uFF0C/u8BF7/u91CD/u8BD5 /!

//配置上传文件过大的错误信息

UploadAction 类package com.test.action;

 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {
 private static final long serialVersionUID = 6150106717429376504L;
 private String username ;
 private String password;
 private List<File> file;
 private List<String> fileFileName;
 private List<String>fileContentType;
 public String getUsername() {
  return username;
 }
 public void setUsername(String username) {
  this.username = username;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 public List<File> getFile() {
  return file;
 }
 public void setFile(List<File> file) {
  this.file = file;
 }
 public List<String> getFileFileName() {
  return fileFileName;
 }
 public void setFileFileName(List<String> fileFileName) {
  this.fileFileName = fileFileName;
 }
 public List<String> getFileContentType() {
  return fileContentType;
 }
 public void setFileContentType(List<String> fileContentType) {
  this.fileContentType = fileContentType;
 }
 @SuppressWarnings("deprecation")
 public String execute() throws Exception {
  String path=ServletActionContext.getRequest().getRealPath("/upload");
  int length=0;
  if(file!=null){
   length=file.size();
  }
  for(int i=0;i<length;i++){
   FileInputStream is=new FileInputStream(file.get(i));
   FileOutputStream os=new FileOutputStream(new File(path,fileFileName.get(i)));
   byte []buffer=new byte[300];
   int len=0;
   while((len=is.read(buffer))>0){
    os.write(buffer, 0, len);
   }
   os.close();
   is.close();
   return SUCCESS;
  }
  return "input";
  
 }
 
}

Uploadlist.jsp

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
 String path = request.getContextPath();
 String basePath = request.getScheme() + "://"
   + request.getServerName() + ":" + request.getServerPort()
   + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <base href="<%=basePath%>">

  <title>My JSP 'upload.jsp' starting page</title>

  <SCRIPT type="text/javascript">
   function addMore(){
    //用JavaScript实现可以上传任意数量的文件
    var td=document.getElementById("more");
    var br=document.createElement("br");
    //产生换行
    var input=document.createElement("input");
    var button=document.createElement("input");
    //产生一个文件输入框
    input.type="file";
    input.name="file";
    //确保name的值与UploadAction的属性,页面file的值的一致
    button.type="button"
    button.value="删除";
     
    td.appendChild(br);
    td.appendChild(input);
    td.appendChild(button);
    
    //给remove 这个按钮注册事件
    button.οnclick=function(){
     //点击删除button可以去除一个输入框

     td.removeChild(br);
     td.removeChild(input);
     td.removeChild(button);
    }
   }
 </SCRIPT>
 </head>

 <body>
  <!--提交方法post,form enctype 改为'multipart/form-data'-->
  <s:form action="upload2" theme="simple" method="post"
   enctype="multipart/form-data" namespace="/test">
   <table border='1' cellpadding="3" bordercolor="blue" cellspacing="3"
    width="420px">
    <caption>上传文件</caption>
    <tr>
     <td>
      username:
     </td>
     <td>
      <s:textfield name="username" cssStyle="width:180px" />
     </td>
    </tr>
    <tr>
     <td>
      password:
     </td>
     <td>
      <s:password name="password" cssStyle="width:180px" />
     </td>
    </tr>
    <tr>
     <td>
      file :
     </td>
     <td id="more">
      <s:file name="file" />
      <input type="button" value="继续添加" οnclick="addMore()"/>
      
     </td>
    </tr>
     <tr>
     <td colspan="2" ><s:fielderror cssStyle="color:red" /></td>

    <!--显示上传出错的信息,配置在message_zh_CN.properties 文件中-->
     </tr>
    <tr>

     <td colspan="2" align="right" >
      <s:submit value="submit" />
     </td>
    </tr>
   </table>
  </s:form>
 </body>
</html>

 

 

uploadresult.jsp

 

//简单的显示提交信息 

<body>
   username :<s:property value="username"></s:property><br/>
   password :<s:property value="password"></s:property><br/>
   fileUpload:
   <ul>
  <s:iterator id="item" value="fileFileName"><li><s:property value="item"/></li></s:iterator>
  </ul>
  <p>上传成功!<a href="uploadlist2.jsp">点击返回继续上传!</a></p>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值