org.apache.struts2.interceptor.FileUploadInterceptor intercept
Content-Type not allowed: attachment "upload_56479d11_12d541899e4__8000_00000001.tmp" image/pjpeg
表单页面为upload.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8"
<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:form action="File_singleUpload" method="post"
</s:form>
</body>
</html>
动作类UploadAction :
package com.allway.luo;
import java.io.File;
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
}
注意:在动作类中有三个属性:attachmentFile,attachmentFileName,attachmentContentType,这三个属性的书写格式是有要求的,格式必须是:[inputName]File,[inputName]FileName,[inutName]ContentType,其中[inputName]就是上传页面里file标签的名字attachment
struts.xml配置文件为:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
<struts>
</struts>
上传成功页面success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
图片上传成功
</body>
</html>
造成此错误的原因是:IE会把 jpg、jpeg翻译成image/pjpeg,png翻译成image/x-png 。而火狐则很标准:jpg、jpeg翻译成image/jpeg,png翻译成image/png。
解决方法:
图片最终是上传的位置为:C:\Documents and Settings\Administrator\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\Test1\iamges;此处Test1为项目名。但是要把上传的图片显示在成功页面获取的方式可以是