Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 文件上载拦截程序Struts2_Java_Jsp_File Upload_Struts2 - Fatal编程技术网

Java 文件上载拦截程序Struts2

Java 文件上载拦截程序Struts2,java,jsp,file-upload,struts2,Java,Jsp,File Upload,Struts2,我是Struts2新手,尝试使用fileUploader拦截器。我正在附加我的所有代码层 操作类(FileUploadAction) package com.caveofprogramming.actions; import java.io.File; import org.apache.struts2.convention.annotation.InterceptorRef; import org.apache.struts2.convention.annotation.Action; i

我是Struts2新手,尝试使用fileUploader拦截器。我正在附加我的所有代码层

操作类(FileUploadAction)

package com.caveofprogramming.actions;
import java.io.File;


import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport{

    private File fileUpload;
    private String fileUploadContentType;
    private String fileUploadFileName;

    public String getFileUploadContentType() {
        return fileUploadContentType;
    }

    public void setFileUploadContentType(String fileUploadContentType) {
        this.fileUploadContentType = fileUploadContentType;
    }

    public String getFileUploadFileName() {
        return fileUploadFileName;
    }

    public void setFileUploadFileName(String fileUploadFileName) {
        this.fileUploadFileName = fileUploadFileName;
    }

    public File getFileUpload() {
        return fileUpload;
    }

    public void setFileUpload(File fileUpload) {
        this.fileUpload = fileUpload;
    }

    @Action( value = "/fileUpload",
            results={@Result(name="success",location="/success.jsp"),
             @Result(name="error",location="/error.jsp"),
             @Result(name="input",location="/error.jsp")
    },
           interceptorRefs={
            @InterceptorRef(
                    params={"allowedTypes","image/jpeg,image/jpg,application/zip",
                            "maximumSize","1024000"}, 
                            value="fileUpload"
                        ),
            @InterceptorRef("defaultStack"),
            @InterceptorRef("validation")
   }
    )
    public String execute(){
        try{
        return SUCCESS;
        } catch(Exception e){
            return ERROR;
        }
    }
    public String display() {
        return NONE;
    }

}
error.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
  <s:fielderror/>
</body>
</html> 
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="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>
Success
</body>
</html>
<%@ taglib prefix="s" uri="/struts-tags" %>

    <html>
    <head>
    <s:head />
    </head>

    <body>
    <h1>Struts 2 &lt;s:file&gt; file upload example</h1>
    <s:form method="post" enctype="multipart/form-data" action="fileUpload">
        <s:file label="File One" name="fileUpload" />
        <s:submit />
    </s:form>

    </body>
    </html>

Success.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
  <s:fielderror/>
</body>
</html> 
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="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>
Success
</body>
</html>
<%@ taglib prefix="s" uri="/struts-tags" %>

    <html>
    <head>
    <s:head />
    </head>

    <body>
    <h1>Struts 2 &lt;s:file&gt; file upload example</h1>
    <s:form method="post" enctype="multipart/form-data" action="fileUpload">
        <s:file label="File One" name="fileUpload" />
        <s:submit />
    </s:form>

    </body>
    </html>

在此处插入标题
成功
fileUpload.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
  <s:fielderror/>
</body>
</html> 
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="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>
Success
</body>
</html>
<%@ taglib prefix="s" uri="/struts-tags" %>

    <html>
    <head>
    <s:head />
    </head>

    <body>
    <h1>Struts 2 &lt;s:file&gt; file upload example</h1>
    <s:form method="post" enctype="multipart/form-data" action="fileUpload">
        <s:file label="File One" name="fileUpload" />
        <s:submit />
    </s:form>

    </body>
    </html>

Struts2S:文件上传示例
我不明白为什么我会犯这个错误

“不允许的内容类型:fileUpload”photography-104a.jpg“upload_37fbf440_169b_4687_af65_93c8c967256c_00000000.tmp”image/pjpeg”


虽然我上传的文件格式是“.jpg”。请帮助我

您遇到此错误可能是因为您不允许使用内容类型为
image/pjpeg
的文件。使用拦截器的参数定义允许的MIME类型

<interceptor-ref name="fileUpload">
    <param name="allowedTypes">image/jpeg,image/pjpeg</param>
</interceptor-ref>  

图像/jpeg,图像/pjpeg
扩展名是
.jpg
,但不是mimetype。