最近刚做完一个项目,用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();//把文件读入