2008-02-19
WebWork文件上传的问题---Content-Type not allowed
今天原本想完成一个webwork实现文件上传的功能,原来的实现很是复杂,另外弹出一个页面,使用apache——upload组件将要上传的图片先提交到服务器,然后这个jsp的页面返回该文件的服务器的路径,提交表单接受这个这个参数,然后提交信息。
现在看这种实现过于复杂,因为种种原因一直没有修改,现在终于提出要修改这个上传实现。
webwork的拦截器实现了文件的上传,这样的例子也很多,很快按照一个例子,在一个测试web程序中部署,顺利通过。
但在应用到现在的网站时候,却出现了一个这样的问题,上传文件被拦截中断执行,报错的异常信息是Content-Type not allowed,无论上传什么类型文件都是这个异常。
下面是全部的代码,这已经是最简单的实现了,但这个测试例子在单独的环境中运行正常,一旦部署到现在的web应用程序中就无法正常运行,查找了很多资料,也专门加了一段content allowed types配置,还是无法正常运行。
两个环境唯一的区别是一个是webwork的2.2.4的版本,一个是webwork2.2的测试版本,由于程序已经很到,很难全部迁移到webwork2.2.4版本上,请教一下,这个bug是不是webwork测试版本的?如果是,除了升级webwork版本外,还有什么办法?如果不是,是不是我的程序写法的原因,在webwork的测试版本是不是有特殊的一些配置和写法?请各位大侠不吝赐教!
页面代码:
<%@ page contentType="text/html; charset=GBK"%>
<%@ taglib prefix="ww" uri="webwork"%>
<html>
<head>
<title>文件上传</title>
</head>
<body>
<form method="POST" name="form1" ENCTYPE="multipart/form-data">
<table width="95%" border="0" cellpadding="5" cellspacing="0">
<tr>
<td>产品图片:</td>
<td align="left" class="xxbg" colspan="2">
<input type="file" name="pic" size="20">
</td>
</tr>
</table>
</form>
</body>
</html>
<SCRIPT LANGUAGE="JavaScript">
function sub(){
form1.action = "uploadtest.action";
form1.submit();}
</SCRIPT>
配置文件:
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" "http://www.opensymphony.com/xwork/xwork-1.1.dtd">
<xwork>
<include file="webwork-default.xml" />
<package name="uploadtest" extends="webwork-default">
<action name="uploadtest"
class="com.shouzhong.test.UplaodAction" method="uploadtest">
<result name="success" type="dispatcher">
<param name="location">
/result.jsp
</param>
</result>
<interceptor-ref name= "fileUpload "/>
<interceptor-ref name= "basicStack "/>
</action>
</package>
</xwork>
类文件:
package com.shouzhong.test;
import java.util.*;
import org.apache.commons.logging.*;
import com.opensymphony.xwork.*;
import com.core.web.Action;
import com.shouzhong.map.*;
import javax.servlet.http.HttpServletRequest;
import com.opensymphony.webwork.ServletActionContext;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* <p>
* Title:
* </p>
* <p>
* Description:
* </p>
* <p>
* Copyright: Copyright (c) 2007
* </p>
* <p>
* Company:
* </p>
*
* @author liuwei
* @version 1.0
*/
public class UplaodAction
extends Action {
public static Log log = LogFactory.getLog(UplaodAction.class);
private File pic;
private String picContentType;
private String picFileName;
public String uploadtest() throws Exception {
try {
System.out.println("in....................");
upload(pic, picFileName);
} catch (Exception e) {
}
return SUCCESS;
}
public static String upload(File file, String filename) {
String ret = null;
try {
if (file != null) {
ret = "c:/" + filename;
FileOutputStream outputStream = new FileOutputStream(ret);
FileInputStream fileIn = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len;
while ((len = fileIn.read(buffer)) > 0) {
outputStream.write(buffer, 0, len);
}
fileIn.close();
outputStream.close();
}
} catch (Exception ex) {
ex.printStackTrace();
ret = null;
}
return ret;
}
public File getPic() {
return pic;
}
public void setPic(File pic) {
this.pic = pic;
}
public String getPicContentType() {
return picContentType;
}
public void setPicContentType(String picContentType) {
this.picContentType = picContentType;
}
public String getPicFileName() {
return picFileName;
}
public void setPicFileName(String picFileName) {
this.picFileName = picFileName;
}
}
发表评论
- 浏览: 54244 次
- 性别:

- 来自: 山西太原

- 详细资料
搜索本博客
我的相册
55_140065_b47aa528cbaf0eb
共 16 张
共 16 张
最新评论
-
卓越网的垃圾处理问题方式
卓越网确实够垃圾,为了这点儿钱搞砸自己的信誉,鼠目寸光的管理者
-- by jones -
卓越网的垃圾处理问题方式
我的订单已经被删除了,我打电话问客服,说什么系统升级,又暂时无货,要我重订。。。 ...
-- by rene103 -
卓越网的垃圾处理问题方式
。不小心成了标题党
-- by liuwei1981 -
卓越网的垃圾处理问题方式
已经改了,呵呵,平时说习惯了,没有想这么多。
-- by liuwei1981 -
卓越网的垃圾处理问题方式
呃……在标题上加个“网”吧 毕竟“卓越”是个褒义的形容词,不小心抱着学习垃圾处理 ...
-- by Friedrich






评论排行榜