上传页面的实现代码如下
upload.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>upload.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<html:form action="" method="post" enctype="multipart/form-data">
<table>
<tr>
<td align="center" colspan="2">
<font size="4">Please Enter the Following Details</font>
</td>
</tr>
<tr>
<td align="left" colspan="2">
<font color="red"><htm:errors /></font>
</td>
</tr>
<tr>
<td align="right">
FileName
</td>
<td align="left">
<html:file property="theFile"></html:file>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<html:submit>Upload File</html:submit>
</td>
</tr>
</table>
</html:form>
</body>
</html:html>
其中struts通过表单实现页面的上传时,需要在form表单把enctype="multipart/form-data",使用了这个属性后,HttpServletRequest对象的getParameter()方法就无法直接得到用户所提交的其他数据。好在struts对这种格式编码的数据进行了处理,使得用户提交的数据仍然可以被直接设置成actionForm种对应的值。
ActionForm:
package ActionForm;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
public class UploadForm extends ActionForm {
private FormFile theFile;
public FormFile getTheFile() {
return theFile;
}
public void setTheFile(FormFile theFile) {
this.theFile = theFile;
}
}
UploadAction文件
package com.xpjjy.struts;
import java.io.File;
import java.io.FileOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
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 org.apache.struts.upload.FormFile;
import ActionForm.UploadForm;
public class UploadAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception{
UploadForm uploadForm = (UploadForm) form;
FormFile file = uploadForm.getTheFile();
String contentType = file.getContentType();
String fileName = file.getFileName();
int fileSize = file.getFileSize();
byte[] fileData = file.getFileData();
System.out.println("Contentype: "+ contentType);
System.out.println("FileName: " + fileName);
System.out.println("File Size "+ fileSize);
FileOutputStream out = new FileOutputStream(new File("D:\\"+fileName));
out.write(fileData);
out.close();
HttpSession session = request.getSession();
session.setAttribute("contentType", contentType);
session.setAttribute("fileName", fileName);
session.setAttribute("fileSize", Integer.valueOf(fileSize));
return mapping.findForward("download");
}
}
httpSession的相关资料空间有
download.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Download and Open the File</title>
</head>
<body>
<html:link action="/download">Download</html:link>
<br>
<html:link action="/open">Open</html:link>
</body>
</html>
DownloadFileAction
package com.xpjjy.struts;
import java.io.File;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DownloadAction;
public class DownloadFileAction extends DownloadAction {
protected StreamInfo getStreamInfo(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
String contentType = (String)session.getAttribute("contentType");
String fileName = (String)session.getAttribute("fileName");
response.setHeader("content-disposition", "attachment; filename="+fileName);
File file = new File("D:\\"+ fileName);
return new FileStreamInfo(contentType,file);
}
}
Strut 的配置文件 struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="uploadForm" type="ActionForm.UploadForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action path="/OutputXMLAction"
type="com.xpjjy.struts.OutputXMLAction"
scope="request"
>
<forward name="success" path="/index.jsp" />
</action>
<action
attribute="uploadForm"
input="/upload.jsp"
name="uploadForm"
path="/upload"
scope="request"
type="com.xpjjy.struts.UploadAction"
>
<forward name="download" path="/download.jsp" />
</action>
<action path="/download"
type="com.xpjjy.struts.DownloadFileAction"
scope="request"
>
</action>
</action-mappings>
<message-resources parameter="com.xpjjy.struts.ApplicationResources" />
</struts-config>
分享到:
相关推荐
struts实现的文件上传下载功能,最大可以上传到150MB
Struts实现上传下载功能 不会的可以进来参考一下
使用struts实现文件的上传下载 代码更好理解
在struts2基础上实现的包括单文件、多文件的上传及下载
struts 文件上传 Struts实现文件上传
用struts实现文件的上传和下载,在实现中,对于文件大小大于3M的进行限定.(注意版本问题)
struts实现图片上传功能。包含详细的代码和配置信息。
struts与hibernate实现文件的上传与动态下载 主要就是运用struts2.2和hibernate3.6进行文件上传与下载,并且能动态的下载,里面包括了详细的构建文档,以及代码
网上的Struts2进行的文件下载一般都是单文件或者固定的文件,并没有(很少)实现随意文件的下载的例子 提供多文件上传,上传成功后,提供刚上传的文件下载功能(其他的都可以在其上面进行扩充) 多文件 上传 下载...
用struts2实现上传控制上传文件类型、大小以及多文件上传
JavaEE Struts文件上传
Struts实现文件的上传下载
一个利用Struts2实现文件的上传下载,使用的是同步方式。没有用ajax请求
struts1和struts2分别实现文件上传下载功能
Struts2实现文件上传的功能,简洁方便,欢迎下载。
Struts Spring Hibernate 上传 下载
struts实现文件上传struts实现文件上传struts实现文件上传
struts1 实现文件上传下载...
参考了phantomes的专栏,解决了其中的FileItem调用问题。 struts2真正实现上传下载完整源代码。 包含lib jar包
struts实现 文件上传和下载,忘大家与我共勉。里面使用的是maven依赖管理,相关的jar也添加在里面。直接放到tomcat webapp中就可以运行