不允许内容类型:Struts 2 中的文件上传 [英] Content-Type not allowed: fileUpload in Struts 2

查看:32
本文介绍了不允许内容类型:Struts 2 中的文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Struts 2 的新手,正在尝试使用 fileUpload 拦截器.我正在附加我所有的代码层

I am new to Struts 2 and trying to do use fileUpload interceptor. I am attaching all my code layers

操作类 (FileUploadAction):

Action Class (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> 

Success.jsp:

<%@ 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>

fileUpload.jsp:

<%@ 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>

我不明白为什么我会收到这个错误

I am not understanding why I am getting this error

"Content-Type not allowed: fileUpload "photography-104a.jpg" "upload_37fbf440_169b_4687_af65_93c8c967256c_00000000.tmp" image/pjpeg"

虽然我上传的文件格式是.jpg.

Although my uploading file format is .jpg.

推荐答案

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

You are getting this error probably because you don't allow files with content type image/pjpeg. Use parameter of fileUpload interceptor to define allowed MIME types

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

这篇关于不允许内容类型:Struts 2 中的文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆