实现struts2的文件上传文件功能

实现多文件上传的前台jsp页面(在WEBROOT的upload文件夹下)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>upload</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<script type="text/javascript">

function addMore(){
//实现多文件上传的JavaScript代码
var moreTd = document.getElementById("more");
var br = document.createElement("br");
var input = document.createElement("input");
input.type="file";
input.name="file";
var button = document.createElement("input");
button.type="button";
button.value="add More ..";

var remove = document.createElement("input");
remove.type="button";
remove.value="remove";

moreTd.appendChild(br);
moreTd.appendChild(input);
moreTd.appendChild(button);
moreTd.appendChild(remove);
button.onclick=addMore;
remove.onclick= function(){
moreTd.removeChild(br);
moreTd.removeChild(input);
moreTd.removeChild(button);
moreTd.removeChild(remove);
}
}
</script>
<s:fielderror></s:fielderror>
<form action="/upload/upload" method="post" enctype="multipart/form-data" >
<table border="1px" align="center" width="50%">
<tr><td>
information:</td><td> <input type="text" name="info">
</td></tr>
<tr><td>
file :</td><td id="more"><input type="file" name="file"/><input type="button" value="add More .." onclick="addMore()"/>
</td></tr>
<tr><td >
<input type="submit" name="submit" ></td><td><input type="reset"/>
</td></tr>
</table>
</form>
</body>
</html>


UploadAction代码如下

package cn.hfut.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport{
private String info;
private File[] file;
private String[] fileFileName;
private String[] fileContentType;
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public File[] getFile() {
return file;
}
public void setFile(File[] file) {
this.file = file;
}
public String[] getFileFileName() {
return fileFileName;
}
public void setFileFileName(String[] fileFileName) {
this.fileFileName = fileFileName;
}
public String[] getFileContentType() {
return fileContentType;
}
public void setFileContentType(String[] fileContentType) {
this.fileContentType = fileContentType;
}
public String execute() throws Exception{
String realPath =
ServletActionContext.getServletContext().getRealPath("/upload");
if(file!=null){
System.out.println(file.length);
for(int i=0;i<this.file.length;i++){
System.out.println(fileFileName[i]);
System.out.println(fileContentType[i]);
File target = new File(realPath,this.fileFileName[i]);
FileOutputStream out = new FileOutputStream(target);
FileInputStream input = new FileInputStream(file[i]);
byte[] temp = new byte[100];
while(input.read(temp)!=-1){
out.write(temp);
}
}
return SUCCESS;
}
return "input";
}
}




在structs.xml文件的配置为<code>
<package name="upload" namespace="/upload" extends="struts-default">

<action name="upload" class="cn.hfut.action.UploadAction">
<result name="input">/upload/upload.jsp</result>
<result>/upload/uploadResult.jsp</result>


<interceptor-ref name="fileUpload">
<param name="allowedExtension">txt,doc</param><!-- 注意此处应与相应的set方法对应,而不是字段 -->
<param name="allowedTypes">text/plain,application/vnd.ms-word</param><!--文件类型-->
<param name="maximumSize">204700</param><!--每个上传文件文件大小-->
</interceptor-ref>
<interceptor-ref name="basicStack"></interceptor-ref>
</action>
</package>
</code>
[color=red]注意:配置中的allowedTypes不是拦截器fileUpload的字段,而是一个属性(有set方法的为属性),不能将写成字段,struts通过反射找到的是属性,二不是字段
[/color]
如果想改变系想改变默认的出错信息(structs.core包中),可以在structs.xml配置一个全局资源文件(如:message.properties)
<code><constant name="struts.custom.i18n.resources" value="message" /> </code>
message.properties文件内容:
struts.messages.error.file.too.large=\u8bf7\u4e0a\u4f20\u5c0f\u4e8e200kb\u7684\u6587\u4ef6
struts.messages.error.content.type.not.allowed=\u53ea\u80fd\u4e0a\u4f20txt\uff0cdoc\u683c\u5f0f\u6587\u4ef6
struts.messages.error.file.extension.not.allowed=\u53ea\u80fd\u4e0a\u4f20txt\uff0cdoc\u683c\u5f0f\u6587\u4ef6

已进行UTF-8编码
[color=red]不过:如果上传多个文件时,如果有一个文件能够上传成功,则不会返回input视图,而是success视图[/color]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值