struts允许上传文件类型设置
just
posted @ 2009年4月01日 05:54
in struts2
, 2092 阅读
UploadAction: 文件上传action
package com.test.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
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;
}
@Override
public String execute() throws Exception {
for(int i=0;i<file.size();i++){
InputStream is = new FileInputStream(file.get(i));
String root = ServletActionContext.getRequest().getRealPath("/upload");
File destfFile = new File(root,this.getFileFileName().get(i));
OutputStream os = new FileOutputStream(destfFile);
byte[] buffer = new byte[400];
int length = 0;
while ((length = is.read(buffer)) > 0) {
os.write(buffer);
}
os.close();
is.close();
}
return SUCCESS;
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
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;
}
@Override
public String execute() throws Exception {
for(int i=0;i<file.size();i++){
InputStream is = new FileInputStream(file.get(i));
String root = ServletActionContext.getRequest().getRealPath("/upload");
File destfFile = new File(root,this.getFileFileName().get(i));
OutputStream os = new FileOutputStream(destfFile);
byte[] buffer = new byte[400];
int length = 0;
while ((length = is.read(buffer)) > 0) {
os.write(buffer);
}
os.close();
is.close();
}
return SUCCESS;
}
}
<action name="upload" class="com.test.action.UploadAction">
<result>/uploadresult.jsp</result>
<result name="input">/upload.jsp</result>
<interceptor-ref name="fileUpload">
<param name="maximumSize">4096000</param>
<param name="allowedTypes">image/jpeg</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
<result>/uploadresult.jsp</result>
<result name="input">/upload.jsp</result>
<interceptor-ref name="fileUpload">
<param name="maximumSize">4096000</param>
<param name="allowedTypes">image/jpeg</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
在拦截器里设置allowedTypes 类型 为允许上传的类型,
具体的类型可以在 tomcat/conf/web.xml中查找
例如:
jpeg 类型:image/jpeg
maximumSize 可限制上传文件的大小
-----------------------------
如果上传的是禁止的类型,struts默认返回的消息不大好,
我们可以自己修改下,
struts默认定义的类型错误消息在org.apache.struts2.struts-messages.properties中的
struts.messages.error.content.type.not.allowed=Content-Type not allowed: {0} "{1}" {2}
我们要修改这个默认的显示,可以在src下建立个message.properties,然后覆盖
struts.messages.error.content.type.not.allowed=上传类型错误
这样显示的信息就不是默认的了
文件大小错误信息也可以这样修改
struts.messages.error.file.too.large=上传文件太大,请重试