先在struts-config.xml里面声明一个DynaActionForm,在做一个关联的Action
<form-bean name="uploadForm" type="org.apache.struts.action.DynaActionForm" dynamic="true">
<form-property name="filename" type="java.lang.String"/>
<form-property name="uploadfile" type="org.apache.struts.upload.FormFile"/>
</form-bean>
<action
attribute="uploadForm"
input="/form/upload.jsp"
name="uploadForm"
path="/upload"
scope="request"
type="com.gcoresoft.struts.action.UploadAction"
/>
Action代码如下

/**//*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.gcoresoft.struts.action;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.upload.FormFile;



/** *//**
* MyEclipse Struts
* Creation date: 05-12-2007
*
* XDoclet definition:
* @struts.action path="/upload" name="Form" attribute="uploadForm" input="/form/upload.jsp" scope="request" validate="true"
*/

public class UploadAction extends Action
{
private static final Log log = LogFactory.getLog(UploadAction.class);

/**//*
* Generated Methods
*/


/** *//**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response)
{
DynaActionForm uploadForm = (DynaActionForm) form;// TODO Auto-generated method stub
String filename=((String)uploadForm.get("filename")).trim();
FormFile file=(FormFile)uploadForm.get("uploadfile");
if(filename.equals("")||filename==null)

{
filename=file.getFileName();

try
{
filename=new String(filename.getBytes("GBK"),"UTF-8");//编码转换

} catch (UnsupportedEncodingException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}

log.debug("Filename:"+filename);
String filepath="e:\\temp\\";
log.debug("Realpath:"+filepath);

try
{
InputStream in=file.getInputStream();
OutputStream out=new FileOutputStream(filepath+filename);
byte[] buffer=new byte[20000];
int n=0;
while((n=in.read(buffer))!=-1)

{
out.write(buffer, 0, n);
}
out.close();
in.close();
log.debug("Upload Successfully");
return mapping.findForward("success");

} catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();

} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return mapping.findForward("failure");
}
}
相关的jsp文件

<%
@ page language="java" pageEncoding="UTF-8"%>

<%
@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>

<%
@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html>
<head>
<title>JSP for DynaActionForm form</title>
</head>
<body>
<html:form action="/upload" enctype = "multipart/form-data">
filename : <html:text property="filename"/><html:errors property="filename"/><br/>
uploadfile : <html:file property="uploadfile"/><html:errors property="uploadfile"/><br/>
<html:submit/><html:cancel/>
</html:form>
</body>
</html>


这样基本就可以了。
还有就是在web.xml文件里面声明了一个Set Character Encoding没效果,不知道怎么回事,呆会晚上问下人看看是什么问题。
这几天查了很多资料都无法解决那个问题,很多都提示需要修改源代码,没办法,先拿这个凑合下吧。
在网上还找到一个方法就是将struts升级到1.2,formfile在1.2中解决了中文乱码这个问题,所以我直接升级到了1.2问题解决。
老bean那里也有一个方法,就是使用
Jakarta 文件上传类,详细资料请参考:
http://www.blogjava.net/beansoft/archive/2007/01/05/92087.html
---------------------------------------------------------
专注移动开发
Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
posted on 2007-05-12 17:20
TiGERTiAN 阅读(1466)
评论(2) 编辑 收藏 所属分类:
Java