JSP页面
<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=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 FileForm form</title>
</head>
<body>
<html:form action="/file"
enctype="multipart/form-data">
上传 :
<html:file property="myFile"/><html:errors name="fileError"/>
<html:submit value="上传"/>
</html:form>
<html:img src="image/dd.jpg"/>
</body>
</html>
/*******************************************************************************/
对应的ActionForm
package wsq.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
/**
* MyEclipse Struts
* Creation date: 09-17-2007
*
* @author 王世清
**/
public class FileForm extends ActionForm {
private static final long serialVersionUID = 1L;
private FormFile myFile;
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
return null;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
public FormFile getMyFile() {
return myFile;
}
public void setMyFile(FormFile myFile) {
this.myFile = myFile;
}
}
/**********************************************************/
对应的Action
package wsq.struts.action;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.upload.FormFile;
import wsq.struts.form.FileForm;
public class FileAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws IOException{
FileForm fileForm = (FileForm) form;
FormFile file = fileForm.getMyFile();
if (file == null)
{
return mapping.findForward("error");
}
String dir=servlet.getServletContext().getRealPath("/image");//首先在你的项目下新建个(在 webroot下)image文件夹
/***注释的是按孙卫琴书上来的*************/
// InputStream in=file.getInputStream();
// OutputStream out=new FileOutputStream(dir+"/"+file.getFileName());
// int bytesRead=0;
// byte [] buffer=new byte[8000];
// while((bytesRead=in.read(buffer, 0, 8000))!=-1)
// {
// out.write(buffer,0,bytesRead);
//
// }
FileOutputStream out = new FileOutputStream(dir+"/" + file.getFileName());
out.write(file.getFileData());
out.close();
//in.close();
file.destroy();
System.out.println(file.getFileName()+"上传文件的名字");
return null;
}
}
/*************************************/
大家还要记得传图片是会出现文件名乱码 解决的办法是写个过滤器
要是不想写可以在
你 的TOMCATE目录下的\webapps\examples\WEB-INF\classes\filters\SetCharacterEncodingFilter.java
TOMCAT也为你写好一个现成的 拿来就可以用了
这两天尝试 用AJAX来写一个文件上传的 呵呵。。。
posted on 2007-09-20 12:06
Crying 阅读(826)
评论(2) 编辑 收藏 所属分类:
Jsp+Struts