一、Struts2 文件上传
使用MVC框架实现上传,很简单。
1、在包org.zttc.itat.action下 新建
MessageAction.java
package org.zttc.itat.action;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.zttc.itat.model.Message;
import com.opensymphony.xwork2.ModelDriven;
public class MessageAction implements ModelDriven<Message>{
private Message msg;
private File photo;
private String photoFileName;
private String photoContentType;
/*
* 会把上传的文件名通过该方法获取
*/
public String getPhotoFileName() {
return photoFileName;
}
/*
* 上传的文件名会通过该方法设置文件名
*/
public void setPhotoFileName(String photoFileName) {
this.photoFileName = photoFileName;
}
public String getPhotoContentType() {
return photoContentType;
}
/*
* 文件名和文件格式采用固定的格式
* xxxFileName,xxxContentType。
*/
public void setPhotoContentType(String photoContentType) {
this.photoContentType = photoContentType;
}
public File getPhoto() {
return photo;
}
public void setPhoto(File photo) {
this.photo = photo;
}
public Message getMsg() {
return msg;
}
public void setMsg(Message msg) {
this.msg = msg;
}
public Message getModel() {
if(msg==null) msg= new Message();
return msg;
}
public String fileInput(){
return "success";
}
public String file(){
try {
System.out.println(photoFileName+","+photoContentType);
FileUtils.copyFile(photo, new File("d:/test/"+photoFileName));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "success";
}
}
2、在org.zttc.itat.model包下 新建
Message.java
package org.zttc.itat.model;
import java.util.Date;
public class Message {
private int id;
private String title;
private String content;
private Date createDate;
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
3、在WEB-INF/Message文件夹下新建
fileInput.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Message FileUpload</h1>
<form action="Message_file.action" method="post" enctype="multipart/form-data">
Title:<input type="text" name="msg.title"/> <br/>
File:<input type="file" name="photo"/> <br/>
<input type="submit" />
</form>
</body>
</html>
4、在Src目录下 新建 struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.action.extension" value="action,ejb,html"/>
<constant name="struts.configuration.xml.reload" value="true"/>
<package name="default" namespace="/" extends="struts-default">
<action name="*_*" class="org.zttc.itat.action.{1}Action" method="{2}">
<result name="success">/WEB-INF/{1}/{2}.jsp</result>
</action>
</package>
</struts>
5、在浏览器输入 http://localhost:8080/struts01_9/Message_fileInput.action并选择上传的文件。
得到:
点击提交,会在D:\test目录下看到java.xml文件。
二、多文件上传
1、改动fileInput.jsp如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Message FileUpload</h1>
<form action="Message_file.action" method="post" enctype="multipart/form-data">
Title:<input type="text" name="msg.title"/> <br/>
File:<input type="file" name="photo"/> <br/>
File:<input type="file" name="photo"/> <br/>
File:<input type="file" name="photo"/> <br/>
<input type="submit" />
</form>
</body>
</html>
2、改动MessageAction.java如下:
package org.zttc.itat.action;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.zttc.itat.model.Message;
import com.opensymphony.xwork2.ModelDriven;
public class MessageAction implements ModelDriven<Message>{
private Message msg;
private File[] photo;
private String[] photoFileName;
private String[] photoContentType;
/*
* 会把上传的文件名通过该方法获取
*/
public String[] getPhotoFileName() {
return photoFileName;
}
/*
* 上传的文件名会通过该方法设置文件名
*/
public void setPhotoFileName(String[] photoFileName) {
this.photoFileName = photoFileName;
}
public String[] getPhotoContentType() {
return photoContentType;
}
/*
* 文件名和文件格式采用固定的格式
* xxxFileName,xxxContentType。
*/
public void setPhotoContentType(String[] photoContentType) {
this.photoContentType = photoContentType;
}
public File[] getPhoto() {
return photo;
}
public void setPhoto(File[] photo) {
this.photo = photo;
}
public Message getMsg() {
return msg;
}
public void setMsg(Message msg) {
this.msg = msg;
}
public Message getModel() {
if(msg==null) msg= new Message();
return msg;
}
public String fileInput(){
return "success";
}
public String file(){
try {
System.out.println(photoFileName+","+photoContentType);
for(int i=0;i<photo.length;i++){
File p =photo[i];
String name=photoFileName[i];
String type=photoContentType[i];
FileUtils.copyFile(p, new File("d:/test/"+name));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "success";
}
}
3、浏览器输入http://localhost:8080/struts01_9/Message_fileInput.action,填入数据,得到
点击提交,完成上传。
三、配置上传文件大小
在struts.xml中添加如下
<constant name="struts.multipart.maxSize" value="10240"/>
10000表示最大上传文件大小为10K。