struts中上传文件-FormFile应用

 

今天花了点时间学习了一下struts的commons-fileupload.jar,在网上找了个例子,用MyEclipse重新开发了一个,
大约用时两个小时,算是ok了!下面是系统应用的部分代码:
UploadsActionAction.java

  //Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.1.1/xslt/JavaClass.xsl

package struts.action;

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 struts.form.UploadsActionForm;
import org.apache.struts.upload.FormFile;
import java.io.*;
/**
* MyEclipse Struts
* Creation date: 06-11-2006
*
* XDoclet definition:
* @struts.action path="/uploadsAction" name="uploadsActionForm"
input="/form/uploadsAction.jsp" scope="request" validate="true"
*/
public class UploadsActionAction extends Action {

// --------------------------------------------------------- Instance Variables

// --------------------------------------------------------- Methods

/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
//UploadsActionForm uploadsActionForm = (UploadsActionForm) form;
public ActionForward execute(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
throws Exception {

String encoding = request.getCharacterEncoding();
if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8")))
{
response.setContentType("text/html; charset=gb2312");//如果没有指定编码,编码格式为gb2312
}
UploadsActionForm theForm = (UploadsActionForm ) form;
FormFile file = theForm.getFiles();//取得上传的文件
try {
InputStream stream = file.getInputStream();//把文件读入
String filePath = request.getRealPath("/upload");//上传到指定的upload包中
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream bos = new FileOutputStream(filePath + "/" +
                               file.getFileName());//建立一个上传文件的输出流
//System.out.println(filePath+"/"+file.getFileName());
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);//将文件写入服务器
}
bos.close();
stream.close();
}catch(Exception e){
System.err.print(e);
}
//request.setAttribute("dat",file.getFileName());
return mapping.findForward("display");

}

}

UploadsActionForm.java

//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.1.1/xslt/JavaClass.xsl

package struts.form;

import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;

/**
* MyEclipse Struts
* Creation date: 06-11-2006
*
* XDoclet definition:
* @struts.form name="uploadsActionForm"
*/
public class UploadsActionForm extends ActionForm {
  //public static final String ERROR_PROPERTY_MAX_LENGTH_EXCEEDED =
"org.apache.struts.webapp.upload.MaxLengthExceeded";

// --------------------------------------------------------- Instance Variables

/** theFile property */
protected FormFile files;

// ---------------------------------------------------------

/**
* Returns files.
* @return FormFile
*/
public FormFile getFiles() {
return files;
}

/**
* Set the files.
* @param files The files to set
*/
public void setFiles(FormFile file) {
this.files = file;
}

}

上传用的页面:uploadFile.jsp

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

<html>
<head>
<title>JSP for uploadsActionForm form</title>
</head>
<body>
<html:form action="/uploadsAction" enctype="multipart/form-data"> <1>
theFile : <html:file property="files"/><br/>
<html:submit/><html:cancel/>
</html:form>
</body>
</html>
刚开始<1>处落下了"enctype="multipart/form-data" 费了我不少时间才搞定.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值