struts2 jpg文件上传失败 Content-Type not allowed

9 篇文章 0 订阅
1 篇文章 0 订阅

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"
    pageEncoding="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"  enctype="multipart/form-data">
   <s:textfield name="description" label="Description"></s:textfield>
   <s:file name="attachment" label="Attachment"></s:file>
   <s:submit/>
</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 {
      private File attachment;
      private String attachmentFileName;
      private String attachmentContentType;
      private String description;
 public File getAttachmentFile() {
  return attachmentFile;
 }
 public void setAttachment(File attachment) {
  this.attachment= attachment;
 }
 public String getAttachmentFileName() {
  return attachmentFileName;
 }
 public void setAttachmentFileName(String attachmentFileName) {
  this.attachmentFileName = attachmentFileName;
 }
 public String getAttachmentContentType() {
  return attachmentContentType;
 }
 public void setAttachmentContentType(String attachmentContentType) {
  this.attachmentContentType = attachmentContentType;
 }
 public String getDescription(){
  return description;
 }
 public void setDescription(String description) {
  this.description = description;
 }
 @Override
 public String execute() throws Exception {
  // TODO Auto-generated method stub
  System.out.println("begin");
  ServletContext servletContext = ServletActionContext.getServletContext();
  if(attachment!=null)
  {
   String dataDir = servletContext.getRealPath("/iamges");
   File saveFile = new File(dataDir,attachmentFileName);
   attachment.renameTo(saveFile);
   System.out.println("success");
  }
  return SUCCESS;
    
}

注意在动作类中有三个属性:attachmentFile,attachmentFileName,attachmentContentType,这三个属性的书写格式是有要求的,格式必须是:[inputName]File,[inputName]FileName,[inutName]ContentType,其中[inputName]就是上传页面里file标签的名字attachment

struts.xml配置文件为:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
 <constant name="struts.i18n.encoding" value="utf-8"/>
 <constant name="struts.devMode" value="true"/>
 <package name="strut2" extends="struts-default">

   <action name="File_singleUpload" class="com.allway.luo.UploadAction">
     <interceptor-ref name="fileUpload">
        <param name="maximumSize">100000</param>
        <param name="allowedTypes">image/gif,image/jpeg,image/png</param>
     </interceptor-ref>
     <interceptor-ref name="basicStack"></interceptor-ref>
     <result name="input">upload.jsp</result>
     <result>success.jsp</result>
   </action>
 </package>
</struts>

上传成功页面success.jsp

 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"   pageEncoding="utf-8"%>

<!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>

图片上传成功

        <img src ='UploadImage/<s:property value ="attachmentFileName" /> ' />

</body>

</html>

 

造成此错误的原因是:IE会把 jpg、jpeg翻译成image/pjpeg,png翻译成image/x-png 。而火狐则很标准:jpg、jpeg翻译成image/jpeg,png翻译成image/png。

解决方法:

     <param name="allowedTypes">
    
 image/bmp,image/png,image/gif,image/jpeg,image/jpg,image/x-png, image/pjpeg
     </param>

图片最终是上传的位置为:C:\Documents and Settings\Administrator\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\Test1\iamges;此处Test1为项目名。但是要把上传的图片显示在成功页面获取的方式可以是   <img src ='iamges/<s:property value ="attachmentFileName" /> ' />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值