我想使用S:file標籤上傳文件,但它給了我http 500內部錯誤。但是當我直接在相同的代碼中將文件名和路徑作爲常量運行時。所以我不確定我在哪裏丟失了循環漏洞。使用s:struts2文件上傳:文件
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ 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>
<title>Struts 2 - Login Application | ViralPatel.net</title>
</head>
<body>
<h2>Struts 2 - Login Application</h2>
<s:actionerror />
<s:form action="uploadFile.action" method="post" enctype="multipart/form-data" >
<s:textfield name="myName"></s:textfield>
<s:file name="myFile"></s:file>
<s:submit value="Submit" align="center" />
</s:form>
</body>
</html>
行動
class FileUploadAction extends ActionSupport implements ServletRequestAware {
File userImage;
String userImageContentType;
String userImageFileName;
HttpServletRequest servletRequest;
public String execute() {
try {
String filePath = servletRequest.getSession()
.getServletContext().getRealPath("/");
File fileToCreate = new File(filePath, this.userImageFileName);
FileUtils.copyFile(this.userImage, fileToCreate);
} catch (Exception e) {
addActionError(e.getMessage());
return INPUT;
}
return SUCCESS;
}
public void setServletRequest(HttpServletRequest servletRequest) {
servletRequest = servletRequest;
}
}
配置
<action name="userImage" class="net.viralpatel.struts2.FileUploadAction">
<interceptor-ref name="fileUpload">
<param name="maximumSize">2097152</param>
<param name="allowedTypes">
image/png,image/gif,image/jpeg,image/pjpeg
</param>
</interceptor-ref>
<interceptor-ref name="defaultStack">
</interceptor-ref>
<result name="success">SuccessUserImage.jsp</result>
<result name="input">UserImage.jsp</result>
</action>
編輯:下一次編輯您的帖子,而不是張貼在留言代碼...
請發佈您的'UploadFile'動作。 – Atropo
你會得到什麼例外? –