Struts实现文件上传

客户端浏览器向WEB服务器上传文件是件比较复杂的事,之前我们有[jsp实现文件上传]和[Servlet中实现文件上传] 这两种方式,我们发现那都需要写很多代码,而使用Struts为我们提供的文件上传机制就简单容易的多了。

首先我们创建一个用于上传文件的JSP表单页。

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Struts文件上传</title>
</head>
<body>
<form action="upload.do" method="post" enctype="multipart/form-data">
文件名:<input type="text" name="fileName"><br>
文件:<input type="file" name="myfile"><br>
<input type="submit">
</form>
</body>
</html>

注意一定要有enctype="multipart/form-data"这个属性,否则无法上传的。

接着是ActionForm。

package cn.ineeke.fileupload.form;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
/**
* 作用:测试学习Struts文件上传
* 作者:Neeke
* BLOG:http://www.ineeke.com
*/
public class UploadActionForm extends ActionForm {
private String fileName;
private FormFile myfile;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public FormFile getMyfile() {
return myfile;
}
public void setMyfile(FormFile myfile) {
this.myfile = myfile;
}
}

这里我们所上传的文件的类型必须使用FormFile!接下来就是Action了。

package cn.ineeke.fileupload.action;
import java.io.FileOutputStream;
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 cn.ineeke.fileupload.form.UploadActionForm;
/**
* 作用:测试学习Struts文件上传
* 作者:Neeke
* BLOG:http://www.ineeke.com
*/
public class UploadAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UploadActionForm upActForm = (UploadActionForm)form;
System.out.println(upActForm.getFileName());//打印出表单中所填写的文件名
FormFile formFile = upActForm.getMyfile();//获取FormFile
if(formFile != null){
System.out.println(formFile.getFileName());//打印出实际上传时所选文件的文件名
FileOutputStream fos = new FileOutputStream("c:\\"+formFile.getFileName());//获取文件流对象
fos.write(formFile.getFileData());//开始写入
fos.flush();
fos.close();
}
return null;
}
}

具体的看旁边的注释应该就够了,万事俱备,只欠配置struts-config.xml了。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="uploadActionForm" type="cn.ineeke.fileupload.form.UploadActionForm"></form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action path="/upload"
type="cn.ineeke.fileupload.action.UploadAction"
name="uploadActionForm"
scope="request"
>
</action>
</action-mappings>
<message-resources parameter="cn.ineeke.struts.ApplicationResources" />
</struts-config>

好了,所有的配置都结束了,可以正常上传文件了。不过,Struts默认的允许上传的文件最大为250M,如果我们只需要10M呢?怎么改?很简单!在struts-config.xml中</action-mappings>标签后增加一句<controller maxFileSize="10M" />即可。


除非另有声明,本站遵循【署名-非商业性使用-相同方式共享 3.0 共享协议】授权。

转载原创文章请注明,转载自:Neeke[http://www.ineeke.com]

本文链接: http://www.ineeke.com/archives/StrutsFileUpload/

2008年12月20日 | 归档于 J2EE技术 | 2 条评论
  1. 2008年12月21日 01:50 | #1

    你博客是什么程序?SEO方面很强,代码美化也很好看哈哈。

    Gravatar头像neeke 于 2008-12-21 12:27:11 回复

    那个啥…传说中的Z-BLOG。

  2. 2008年12月20日 21:26 | #2

    亲爱的哥们:Java怎么样才能学习好啊,很让我头痛。特别是类、方法、构造方法、继承、接口、包…这几个搞的我是头昏脑胀。Java太难学了。哥们有什么好的方法或者什么教程啊!

    Gravatar头像neeke 于 2008-12-21 12:30:09 回复

    我觉得不难啊…只要搞清楚了“面向对象”就都清楚了(还是比较抽象的,我都不知道我是否真的搞明白了。O(∩_∩)O哈哈~),我们现实生活中很多就是面向对象的,相互联系一下你就明白了。

发表评论

XHTML: 您可以使用这些标签: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">
n:-zy n:-zr n:-zan n:-xf n:-wx n:-tz n:-tt n:-ts n:-sy n:-st n:-ss n:-sk n:-qd n:-pz n:-lh n:-kun n:-ku n:-hx n:-hd n:-gt n:-gg n:-bz

NOTICE: You should type some Chinese word (like “你好”) in your comment to pass the spam-check, thanks for your patience!