JSP代码: <s:form action="login/upload" method="post" enctype="multipart/form-data">
<s:textfield name="username" label="用户名"/>
<s:file name="file" label="文件"/>
<s:file name="file" label="文件2"/>
<s:file name="file" label="文件3"/>
<s:submit value="提交"></s:submit>
</s:form>
Action代码:package com.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 static final long serialVersionUID = 1L;
private final static String UPLOADDIR = "/upload";
private String username;
private List<File> file; private List<String> fileFileName; 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;
} private List<String> fileContentType; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public List<File> getFile() {
return file;
} public void setFile(List<File> file) {
this.file = file;
} @Override
public String execute() throws Exception {
upload();
return super.execute();
} private void upload() throws Exception {
for (int i = 0; i < file.size(); i++) {
InputStream in = new FileInputStream(file.get(i));
String dir = ServletActionContext.getRequest().getRealPath(
UPLOADDIR); File uploadFile = new File(dir, this.getFileFileName().get(i));
OutputStream out=new FileOutputStream(uploadFile);
byte[] buffer=new byte[1024*1024];
int length;
while((length=in.read(buffer))>0){
out.write(buffer,0,length);
}
out.close();
in.close();
}
}}
struts.xml:<?xml version="1.0" encoding="GBK" ?>
<!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.devMode" value="false" />-->
<constant name="struts.i18n.encoding" value="GBK"/>
<constant name="struts.custom.i18n.resources" value="app"/>
<!-- 系统常量定义,定义上传文件临时存放路径 -->
<constant name="struts.multipart.saveDir" value="c:\"></constant>
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="loginInterceptor" class="com.interceptor.LoginInterceptor"/>
<interceptor-stack name="myStack">
<interceptor-ref name="loginInterceptor"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<global-results>
<result name="login">/index.jsp</result>
</global-results>
<action name="loginAction" class="com.action.LoginAction">
<result name="success">/Login_Success.jsp </result>
<result name="input">/index.jsp</result>
<result name="faile">/index.jsp</result>
</action> <action name="languageAction" class="com.action.LanguageAction">
<result>/index.jsp</result>
</action>
<action name="registerAction" class="com.action.RegisterAction">
<result type="dispatcher">/Register_Success.jsp</result>
<result name="input">/register.jsp</result>
</action>
<action name="register">
<result>/register.jsp</result>
</action>
<action name="test" class="com.action.TestLoginAction">
<result>/register.jsp</result>
<interceptor-ref name="myStack"/>
</action>
<action name="upload" class="com.action.UploadAction">
<result name="success">/upload_success.jsp</result>
<!--<result name="input">/upload.jsp</result>-->
<interceptor-ref name="myStack"/>
<interceptor-ref name="fileUpload">
<param name="maximumSize">1024*1024</param>
<param name="allowedTypes">image/gif,image/jpeg</param>
</interceptor-ref>
</action>
</package>
</struts>
不仅文件的类型控制没用,大小限制也没用,哪个高手帮我分析分析,谢谢了
<s:textfield name="username" label="用户名"/>
<s:file name="file" label="文件"/>
<s:file name="file" label="文件2"/>
<s:file name="file" label="文件3"/>
<s:submit value="提交"></s:submit>
</s:form>
Action代码:package com.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 static final long serialVersionUID = 1L;
private final static String UPLOADDIR = "/upload";
private String username;
private List<File> file; private List<String> fileFileName; 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;
} private List<String> fileContentType; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public List<File> getFile() {
return file;
} public void setFile(List<File> file) {
this.file = file;
} @Override
public String execute() throws Exception {
upload();
return super.execute();
} private void upload() throws Exception {
for (int i = 0; i < file.size(); i++) {
InputStream in = new FileInputStream(file.get(i));
String dir = ServletActionContext.getRequest().getRealPath(
UPLOADDIR); File uploadFile = new File(dir, this.getFileFileName().get(i));
OutputStream out=new FileOutputStream(uploadFile);
byte[] buffer=new byte[1024*1024];
int length;
while((length=in.read(buffer))>0){
out.write(buffer,0,length);
}
out.close();
in.close();
}
}}
struts.xml:<?xml version="1.0" encoding="GBK" ?>
<!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.devMode" value="false" />-->
<constant name="struts.i18n.encoding" value="GBK"/>
<constant name="struts.custom.i18n.resources" value="app"/>
<!-- 系统常量定义,定义上传文件临时存放路径 -->
<constant name="struts.multipart.saveDir" value="c:\"></constant>
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="loginInterceptor" class="com.interceptor.LoginInterceptor"/>
<interceptor-stack name="myStack">
<interceptor-ref name="loginInterceptor"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<global-results>
<result name="login">/index.jsp</result>
</global-results>
<action name="loginAction" class="com.action.LoginAction">
<result name="success">/Login_Success.jsp </result>
<result name="input">/index.jsp</result>
<result name="faile">/index.jsp</result>
</action> <action name="languageAction" class="com.action.LanguageAction">
<result>/index.jsp</result>
</action>
<action name="registerAction" class="com.action.RegisterAction">
<result type="dispatcher">/Register_Success.jsp</result>
<result name="input">/register.jsp</result>
</action>
<action name="register">
<result>/register.jsp</result>
</action>
<action name="test" class="com.action.TestLoginAction">
<result>/register.jsp</result>
<interceptor-ref name="myStack"/>
</action>
<action name="upload" class="com.action.UploadAction">
<result name="success">/upload_success.jsp</result>
<!--<result name="input">/upload.jsp</result>-->
<interceptor-ref name="myStack"/>
<interceptor-ref name="fileUpload">
<param name="maximumSize">1024*1024</param>
<param name="allowedTypes">image/gif,image/jpeg</param>
</interceptor-ref>
</action>
</package>
</struts>
不仅文件的类型控制没用,大小限制也没用,哪个高手帮我分析分析,谢谢了
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货