現在、アップロードインターセプターを学習しています。ドキュメントによると、すべてのアップロード ファイルのサイズが定数「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
例外をスローできないのはなぜですか? 入力ビューに移動しないのはなぜですか?