我现在正在学习上传拦截器。文档说,所有上传文件的大小超过常量“struts.multipart.maxSize”的值都会在后台抛出异常,视图会进入INPUT视图。但是,在我的情况下,它不能抛出任何异常,并且视图没有进入 INPUT 视图(上传进度一直是 0%,见图)。
这是我上传的 html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!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>HELLO!${session.user }</h1>
<div align="center">
<s:form method="post" enctype="multipart/form-data" action="upload">
<s:fielderror></s:fielderror>
<s:file label="上传文件" name="upload" />
<s:file label="上传文件" name="upload" />
<s:file label="上传文件" name="upload" />
<s:submit />
</s:form>
</div>
</body>
</html>
这是我的行动的代码
package com.aks.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.List;
import java.util.UUID;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = -6671906825564499267L;
private static final int BUFFER_SIZE = 4096;
private List<File> upload;
private List<String> uploadFileName;
private List<String> uploadContentType;
private String savePath;
public String getSavePath() {
return ServletActionContext.getRequest().getServletContext().getRealPath(this.savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public List<File> getUpload() {
return upload;
}
public void setUpload(List<File> upload) {
this.upload = upload;
}
public List<String> getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(List<String> uploadFileName) {
this.uploadFileName = uploadFileName;
}
public List<String> getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(List<String> uploadContentType) {
this.uploadContentType = uploadContentType;
}
@Override
public String execute() throws Exception {
String newFileName = (UUID.randomUUID() + uploadFileName.get(0).substring(uploadFileName.get(0).lastIndexOf(".")));
System.out.println(newFileName);
System.out.println(getSavePath());
FileInputStream fis = new FileInputStream(upload.get(0));
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + newFileName);
System.out.println(getSavePath());
FileChannel fcIn = fis.getChannel();
FileChannel fcOut = fos.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
while(true){
buffer.clear();
int r = fcIn.read(buffer);
if(r == -1){
break;
}
buffer.flip();
fcOut.write(buffer);
}
getUploadFileName().set(0, newFileName);
fcIn.close();
fcOut.close();
fis.close();
fos.close();
return SUCCESS;
}
}
这是结果html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!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>显示上传图片</title>
</head>
<body>
<img src="upload/${uploadFileName['0']} " />
<s:debug></s:debug>
</body>
</html>
最后就是这个动作的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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<constant name="struts.multipart.maxSize" value="100000000" /> <!-- 20MB -->
<constant name="struts.multipart.saveDir" value="/temp" />
<package name="default" namespace="/" extends="struts-default">
<action name="index">
<result name="success">index.jsp</result>
</action>
<action name="login" class="com.aks.action.LoginAction">
<result name="input">index.jsp</result>
<result name="success">/WEB-INF/jsp/welcome.jsp</result>
<result name="error">/WEB-INF/jsp/error.jsp</result>
</action>
<action name="upload" class="com.aks.action.UploadAction">
<interceptor-ref name="defaultStack">
<param name="fileUpload.allowedTypes">image/png,image/jpeg,application/x-zip-compressed,application/octet-stream</param>
<param name="fileUpload.maximumSize">90000000</param>
</interceptor-ref>
<param name="savePath">/upload</param>
<result name="input">index.jsp</result>
<result name="success">/WEB-INF/jsp/showImage.jsp</result>
<result name="error">/WEB-INF/jsp/error.jsp</result>
</action>
</package>
<!-- Add packages here -->
</struts>
我的开发环境:eclipse4.5.1+tomcat8+jdk1.8+struts2.3.24
为什么不能抛出异常,为什么不去输入视图?