Struts的文件上传

王朝other·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

最近刚做完一个项目,用Struts1.1做的。从不懂,到熟练使用,都靠参考CSDN的一些文档。但是文章上讲的并不一定适合自己,所以我把我自己做的一些东西拿上来给大家看看,互相交流一下。假如您有跟好的方法,可以和我联系。

MSN:whw_dream (AT) hotmail.com

Struts的文件上传

本文用的是Struts1.1的org.apache.struts.upload.FormFile类。很方便,不用自己写。也不用写一个jsp调用jspsmartupload就可以搞定。

选择上传文件页面:selfile.jsp

<%@ taglib uri="/WEB-INF/struts-Html.tld" prefix="html"%>

<html:html>

<html:form action="/uploadsAction.do" enctype="multipart/form-data">

<html:file property="theFile"/>

<html:submit/>

</html:form>

</html:html>

UpLoadAction.Java

import java.io.*;

import javax.servlet.http.*;

import org.apache.struts.action.*;

import org.apache.struts.upload.FormFile;

/**

* <p>Title:UpLoadAction</p>

* <p>Description: QRRSMMS </p>

* <p>Copyright: Copyright (c) 2004 jiahansoft</p>

* <p>Company: jiahansoft</p>

* @author wanghw

* @version 1.0

*/

public class UpLoadAction extends Action {

public ActionForward execute(ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response)

throws Exception {

if (form instanceof uploadsForm) {//假如form是uploadsForm

String encoding = request.getCharacterEncoding();

if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8")))

{

response.setContentType("text/html; charset=gb2312");//假如没有指定编码,编码格式为gb2312

}

UpLoadForm theForm = (UpLoadForm ) form;

FormFile file = theForm.getTheFile();//取得上传的文件

try {

InputStream stream = file.getInputStream();//把文件读入

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航