当前页面: 开发资料首页 → Java 专题 → struts中的文件上传
struts中的文件上传
摘要: struts中的文件上传
</td>
</tr>
<tr>
<td height="35" valign="top" class="ArticleTeitle">
这是学struts1.2.4自带的例子。所有文件及目录结构请在本站下载。
<table width="713" border="0">
<tr>
<td width="435">
一、web.xml配置,这里将文件上传配置为upload模块。
<?xml version="1.0" encoding="iso-8859-1"?>
web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
Struts Blank Application
action
org.apache.struts.action.ActionServlet
config
/WEB-INF/struts-config.xml
config/upload
/WEB-INF/upload/struts-config.xml
</td>
<td width="268" valign="top">
</td>
</tr>
</table>
debug
2
detail
2
2
action
*.do
index.jsp
/tags/struts-bean
/WEB-INF/struts-bean.tld
/tags/struts-html
/WEB-INF/struts-html.tld
/tags/struts-logic
/WEB-INF/struts-logic.tld
/tags/struts-nested
/WEB-INF/struts-nested.tld
/tags/struts-tiles
/WEB-INF/struts-tiles.tld
--------------------------------------------------------------------------------
二、struts-config.xml配置和资源文件
--------------------------------------------------------------------------------
<?xml version="1.0" encoding="iso-8859-1"?>
struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://struts.apache.org/dtds/struts-config_1_2.dtd">
<form-beans>
<form-bean name="uploadForm" type="org.apache.struts.webapp.upload.UpLoadForm" />
</form-beans>
type="org.apache.struts.webapp.upload.UpLoadAction"
name="uploadForm" scope="request" input="input">
资源文件:UploadResources_zh_CN.properties
maxLengthExceeded=已经超过了上传文件所允许的最大值。
maxLengthExplanation=注意:这个应用程序允许上传文件的最大值是2M。请看"/WEB-INF/upload/struts-config.xml" 文件更改这个值。
三、选择上传文件页面:selfile.jsp,如此访问此页面:
继续上传
--------------------------------------------------------------------------------
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="org.apache.struts.action.*,
java.util.Iterator,
org.apache.struts.Globals" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
--------------------------------------------------------------------------------
四、表单bean: UpLoadForm.java
--------------------------------------------------------------------------------
package org.apache.struts.webapp.upload;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
import org.apache.struts.upload.*;
/**
*
Title:UpLoadForm
*
Description: QRRSMMS
*
Copyright: Copyright (c) 2004 jiahansoft
*
Company: jiahansoft
* @author wanghw
* @version 1.0
*/
public class UpLoadForm extends ActionForm {
public static final String ERROR_PROPERTY_MAX_LENGTH_EXCEEDED = "org.apache.struts.webapp.upload.MaxLengthExceeded";
protected FormFile theFile;
public FormFile getTheFile() {
return theFile;
}
public void setTheFile(FormFile theFile) {
this.theFile = theFile;
}
public ActionErrors validate(
ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = null;
//has the maximum length been exceeded?
Boolean maxLengthExceeded =
(Boolean) request.getAttribute(
MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
if ((maxLengthExceeded != null) && (maxLengthExceeded.booleanValue())) {
errors = new ActionErrors();
errors.add(
ActionMessages.GLOBAL_MESSAGE ,
new ActionMessage("maxLengthExceeded"));
errors.add(
ActionMessages.GLOBAL_MESSAGE ,
new ActionMessage("maxLengthExplanation"));
}
return errors;
}
}
--------------------------------------------------------------------------------
五、处理上传的文件:UpLoadAction.java
--------------------------------------------------------------------------------
package org.apache.struts.webapp.upload;
import java.io.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.upload.FormFile;
/**
*
Title:UpLoadAction
*
Description: QRRSMMS
*
Copyright: Copyright (c) 2004 jiahansoft
*
Company: jiahansoft
* @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 UpLoadForm) {//如果form是UpLoadsForm
String encoding = request.getCharacterEncoding();
if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8")))
{
response.setContentType("text/html; charset=gb2312");
}
UpLoadForm theForm = (UpLoadForm ) form;
FormFile file = theForm.getTheFile();//取得上传的文件
String contentType = file.getContentType();
String size = (file.getFileSize() + " bytes");//文件大小
String fileName= file.getFileName();//文件名
try {
InputStream stream = file.getInputStream();//把文件读入
String filePath = request.getRealPath("/");//取当前系统路径
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream bos = new FileOutputStream(filePath + "/" +
file.getFileName());
//建立一个上传文件的输出流,将上传文件存入web应用的根目录。
//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());
request.setAttribute("contentType", contentType);
request.setAttribute("size", size);
request.setAttribute("fileName", fileName);
return mapping.findForward("display");
}
return null;
}
}
------------------------------------------------------------------------------------------
五、成功页display.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="org.apache.struts.action.*,
java.util.Iterator,
org.apache.struts.Globals" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
上传成功!上传信息如下:
The File name: <%= request.getAttribute("fileName") %>
The File content type: <%= request.getAttribute("contentType") %>
The File size: <%= request.getAttribute("size") %>
继续上传
六、测试
从本站下载整个目录结构TestStruts并放入tomcat的webapps目录下,在浏览器中输入:
http://127.0.0.1:8080/TestStruts/upload/upload.do
function TempSave(ElementID)
{
CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value);
CommentsPersistDiv.save("CommentXMLStore");
}
function Restore(ElementID)
{
CommentsPersistDiv.load("CommentXMLStore");
document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent");
}
</td>
</tr>
<tr>
↑返回目录
前一篇: struts编程学习-标签logic:iterate
后一篇: 在struts中用validator作服务器端验证