Posted on 2007-08-26 18:26
hilor 阅读(1571)
评论(0) 编辑 收藏 所属分类:
Struts
今天花了点时间学习了一下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" 费了我不少时间才搞定
--------------------------------------------------------------------------------------------------------------
FormBean:
package onlyfun.caterpillar;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.upload.*;
public class UploadForm extends ActionForm {
private FormFile file;
public void setFile(FormFile file) {
this.file = file;
}
public FormFile getFile() {
return file;
}
public void reset(ActionMapping mapping, HttpServletRequest req) {
file = null;
}
}
JSP:
<html:form action="/Upload" method="post" enctype="multipart/form-data">
選擇檔案:<html:file property="file" />
<html:submit>上傳</html:submit>
</html:form>
Action:
package onlyfun.caterpillar;
import java.io.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.upload.*;
public class UploadAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
UploadForm fileForm = (UploadForm) form;
FormFile file = fileForm.getFile();
FileOutputStream fileOutput = new FileOutputStream("/home/caterpillar/files/" + file.getFileName());
fileOutput.write(file.getFileData());
fileOutput.flush();
fileOutput.close();
return mapping.findForward("success");
}
}