`
- 浏览:
127512 次
- 性别:
-
web.xml配置:
- <?xmlversion="1.0"encoding="UTF-8"?>
- <web-appversion="2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>
- org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
- </filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>*.action</url-pattern>
- </filter-mapping>
- </web-app>
struts.xml配置:
- <?xmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPEstrutsPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.1//EN""http://struts.apache.org/dtds/struts-2.1.dtd">
- <struts>
- <constantname="struts.i18n.encoding"value="utf-8"/>
- <packagename="file"extends="struts-default">
- <actionname="showUpload">
- <result>/upload.jsp</result>
- </action>
- <actionname="upload"class="com.boxun.hzw.action.UploadAction">
- <resultname="input">/upload.jsp</result>
- <result>/download.jsp</result>
- <interceptor-refname="fileUpload">
- <paramname="maximumSize">2097152</param>
- <paramname="addowedTypes">image/bmp,image/jpg</param>
- </interceptor-ref>
- <interceptor-refname="defaultStack"></interceptor-ref>
- </action>
- <actionname="download"class="com.boxun.hzw.action.DownloadAction">
- <resultname="success"type="stream">
- <paramname="contentDisposition">attachment;filename="${fileName}"</param>
- <paramname="inputName">downloadFile</param>
- </result>
- </action>
- </package>
- </struts>
后台实体类代码:
- packagecom.boxun.hzw.bean;
- publicclassUploadFiles{
- privateStringuploadContentType;
- privateStringuploadFileName;
- privateStringuploadRealName;
- publicStringgetUploadContentType(){
- returnuploadContentType;
- }
- publicvoidsetUploadContentType(StringuploadContentType){
- this.uploadContentType=uploadContentType;
- }
- publicStringgetUploadFileName(){
- returnuploadFileName;
- }
- publicvoidsetUploadFileName(StringuploadFileName){
- this.uploadFileName=uploadFileName;
- }
- publicStringgetUploadRealName(){
- returnuploadRealName;
- }
- publicvoidsetUploadRealName(StringuploadRealName){
- this.uploadRealName=uploadRealName;
- }
- }
上传Action类:
- packagecom.boxun.hzw.action;
- importjava.io.*;
- importjava.util.ArrayList;
- importjava.util.List;
- importjava.util.UUID;
- importorg.apache.commons.io.FileUtils;
- importorg.apache.struts2.ServletActionContext;
- importcom.boxun.hzw.bean.UploadFiles;
- importcom.opensymphony.xwork2.ActionContext;
- importcom.opensymphony.xwork2.ActionSupport;
- @SuppressWarnings("all")
- publicclassUploadActionextendsActionSupport{
- privatestaticfinallongserialVersionUID=1L;
- privateFile[]upload;
- privateString[]uploadContentType;
- privateString[]uploadFileName;
- privateList<UploadFiles>uploadFiles=newArrayList<UploadFiles>();
- publicStringexecute(){
- StringtargetDirectory=ServletActionContext.getServletContext().getRealPath("/"+"images/");
- for(inti=0;i<upload.length;i++){
- StringfileName=uploadFileName[i];
- Stringtype=uploadContentType[i];
- StringrealName=UUID.randomUUID().toString()+
- getExt(fileName);
- Filetarget=newFile(targetDirectory,realName);
- try{
- FileUtils.copyFile(upload[i],target);
- }catch(IOExceptione){
- e.printStackTrace();
- returnINPUT;
- }
- UploadFilesuf=newUploadFiles();
- uf.setUploadContentType(type);
- uf.setUploadFileName(fileName);
- uf.setUploadRealName(realName);
- uploadFiles.add(uf);
- }
- ServletActionContext.getRequest().setAttribute("uploadFiles",uploadFiles);
- returnSUCCESS;
- }
- publicFile[]getUpload(){
- returnupload;
- }
- publicvoidsetUpload(File[]upload){
- this.upload=upload;
- }
- publicString[]getUploadContentType(){
- returnuploadContentType;
- }
- publicvoidsetUploadContentType(String[]uploadContentType){
- this.uploadContentType=uploadContentType;
- }
- publicString[]getUploadFileName(){
- returnuploadFileName;
- }
- publicvoidsetUploadFileName(String[]uploadFileName){
- this.uploadFileName=uploadFileName;
- }
- publicList<UploadFiles>getUploadFiles(){
- returnuploadFiles;
- }
- publicvoidsetUploadFiles(List<UploadFiles>uploadFiles){
- this.uploadFiles=uploadFiles;
- }
- publicstaticlonggetSerialversionuid(){
- returnserialVersionUID;
- }
- publicstaticStringgetExt(StringfileName){
- returnfileName.substring(fileName.lastIndexOf("."));
- }
- }
下载Action类:
- packagecom.boxun.hzw.action;
- importjava.io.InputStream;
- importjava.io.UnsupportedEncodingException;
- importorg.apache.struts2.ServletActionContext;
- importcom.opensymphony.xwork2.ActionSupport;
- @SuppressWarnings("all")
- publicclassDownloadActionextendsActionSupport{
- privatestaticfinallongserialVersionUID=6329383258366253255L;
- privateStringfileName;
- privateStringfileRealName;
- publicvoidsetFileName(){
- Stringfname=ServletActionContext.getRequest().getParameter("name");
- Stringfrealname=ServletActionContext.getRequest().getParameter("realname");
- try{
- fname=newString(fname.getBytes("ISO-8859-1"),"utf-8");
- frealname=newString(frealname.getBytes("ISO-8859-1"),"utf-8");
- }catch(UnsupportedEncodingExceptione){
- e.printStackTrace();
- }
- this.fileName=fname;
- this.fileRealName=frealname;
- }
- publicStringgetFileName(){
- try{
- fileRealName=newString(fileRealName.getBytes(),"ISO-8859-1");
- }catch(UnsupportedEncodingExceptione){
- e.printStackTrace();
- }
- returnfileRealName;
- }
- publicInputStreamgetDownloadFile(){
- this.setFileName();
- returnServletActionContext.getServletContext().getResourceAsStream("/"+"images/"+fileRealName);
- }
- @Override
- publicStringexecute()throwsException{
- returnSUCCESS;
- }
- }
上传jsp页面:
- <%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
- <%
- Stringpath=request.getContextPath();
- StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
- <html>
- <head>
- <basehref="<%=basePath%>">
- <title>MyJSP'upload.jsp'startingpage</title>
- <metahttp-equiv="pragma"content="no-cache">
- <metahttp-equiv="cache-control"content="no-cache">
- <metahttp-equiv="expires"content="0">
- <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
- <metahttp-equiv="description"content="Thisismypage">
- </head>
- <body>
- <formaction="upload.action"method="post"enctype="multipart/form-data">
- <table>
- <tr>
- <td>上传文件</td>
- <td>标题:<inputtype="text"name="uploadFileName"/>
- <inputtype="file"name="upload"/><br/>
- 标题:<inputtype="text"name="uploadFileName"/>
- <inputtype="file"name="upload"/><br/>
- 标题:<inputtype="text"name="uploadFileName"/>
- <inputtype="file"name="upload"/><br/>
- 标题:<inputtype="text"name="uploadFileName"/>
- <inputtype="file"name="upload"/><br/>
- 标题:<inputtype="text"name="uploadFileName"/>
- <inputtype="file"name="upload"/>
- </td>
- </tr>
- <tr>
- <td><inputtype="submit"value="提交"/></td>
- <td><inputtype="reset"value="重置"/></td>
- </tr>
- </table>
- </form>
- </body>
- </html>
下载jsp页面:
- <%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
- <%@taglibprefix="c"uri="http://java.sun.com/jsp/jstl/core"%>
- <%
- Stringpath=request.getContextPath();
- StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
- <html>
- <head>
- <basehref="<%=basePath%>">
- <title>MyJSP'download.jsp'startingpage</title>
- <metahttp-equiv="pragma"content="no-cache">
- <metahttp-equiv="cache-control"content="no-cache">
- <metahttp-equiv="expires"content="0">
- <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
- <metahttp-equiv="description"content="Thisismypage">
- </head>
- <body>
- <c:forEachitems="${uploadFiles}"var="files">
- <imgsrc="images/${files.uploadRealName}"mce_src="images/${files.uploadRealName}"alt="ds"width="200px"height="300px"/>
- <ahref="download.action?name=${files.uploadFileName}&realname=${files.uploadRealName}"mce_href="download.action?name=${files.uploadFileName}&realname=${files.uploadRealName}">${files.uploadFileName}</a><br/>
- </c:forEach>
- </body>
- </html>
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
Struts2框架是Java Web开发中的一个流行MVC(Model-View-Controller)框架,它提供了丰富的功能,包括处理表单提交、文件上传等。在Struts2中,文件上传是一个常见的需求,可以帮助用户从客户端上传文件到服务器。...
在Struts框架中,文件上传和下载是常见的功能需求,尤其在处理用户数据交互时。 一、文件上传 1. Struts1中的文件上传:在Struts1中,文件上传主要依赖于`commons-fileupload`和`commons-io`两个库。首先需要在`...
本篇文章将详细探讨如何在Struts2框架下实现文件的上传与下载。 首先,我们需要了解Struts2中的文件上传机制。Struts2提供了`FileUploadInterceptor`拦截器来处理文件上传请求。在处理文件上传时,开发者需要在...
在本系统中,"struts框架写的文件上传下载系统"是利用Struts框架来实现文件的上传和下载功能,提供了一个平台让用户能够注册成为会员后进行文件操作。 首先,让我们深入理解Struts框架的核心概念: 1. **模型...
在本项目中,我们将学习如何利用Struts框架实现多文件上传的功能,这对于初学者来说是一个很好的实践项目,能够帮助理解和掌握Struts的核心概念。 首先,了解文件上传的基本原理。在Web应用中,文件上传通常涉及到...
以上就是SSH框架中使用Struts2和Hibernate实现图片上传的主要知识点,涵盖了Web请求处理、ORM框架、文件上传、数据库操作以及前端交互等多个方面。实际项目开发时,还需要结合具体的业务需求和安全规范进行详细设计...
在Struts2中实现文件上传,可以帮助开发者处理用户从浏览器端上传的文件,例如图片、文档等。以下是关于Struts2文件上传的详细知识点: 1. **依赖库**: 实现文件上传,首先需要引入Struts2的上传插件,即`struts2...
通过以上步骤,你可以实现一个基于Struts2和Hibernate的文件上传与动态下载系统。这个系统能够处理用户上传的文件,将其保存到服务器,同时提供动态下载功能,允许用户根据需要下载文件。在实际开发中,还需要考虑...
本文主要探讨如何将Struts2框架与Spring框架进行整合,并应用于文件上传下载功能的实现。 #### 二、Struts2框架概述 ##### 2.1 Struts2框架简介 Struts2是由Apache软件基金会维护的一个开源项目,它是一个基于...
总结起来,使用Struts实现文件上传下载涉及前端表单设计、后端处理逻辑、文件存储策略以及安全控制等多个方面。在实践中,我们还需要考虑到性能优化和用户体验提升,例如使用异步上传、进度条展示等技术。
下面将详细介绍如何利用SWFUpload与Struts2来实现多文件上传。 **一、SWFUpload组件介绍** SWFUpload 是一个JavaScript库,它利用Flash技术提供了一个高级的文件上传体验。它的主要特性包括: 1. **多文件选择**...
在这个“Struts框架文件上传下载案例”中,我们将探讨如何在Struts2框架下实现文件的上传和下载功能,这对于Web应用来说是非常常见的需求。 文件上传功能是Web应用中允许用户从本地计算机选择文件并将其传输到...
这篇博客文章提供的"struts2文件上传下载源代码"旨在帮助开发者理解和实现这些功能。 文件上传功能允许用户从他们的设备上传文件到服务器。在Struts2中,这通常通过表单实现,表单包含一个`<input type="file">`...
Struts2是一个强大的MVC(Model-View-Controller)框架,广泛应用于Java Web开发中,提供了丰富的功能,包括文件的上传和下载。在本项目中,我们关注的是如何使用Struts2来实现实时的文件交互操作,即文件的上传与...
Struts1和Struts2是两个非常著名的Java Web框架,它们都提供了处理文件上传和下载的功能,但实现方式有所不同。本文将深入探讨这两个框架在文件操作方面的具体实现。 首先,让我们了解一下Struts1中的文件上传功能...
Struts2提供了完善的文件上传支持,让我们来详细探讨如何在Struts2中实现多文件上传。 首先,我们需要在Struts2的配置文件(struts.xml)中启用文件上传的支持。这通常涉及到添加`<constant>`标签来设置`struts....
在这个特定的项目中,我们关注的是"struts2文件上传下载"的功能,这涉及到用户通过Web界面上传文件到服务器,以及从服务器下载文件到用户的设备。 文件上传是Web应用中的常见需求,例如用户可能需要提交图片、文档...
在Struts2框架下实现Uploadify多文件上传,我们需要考虑以下几个关键知识点: 1. **Struts2 Action配置**: 在Struts2中,你需要创建一个Action类来处理文件上传请求。这个Action类需要继承`org.apache.struts2....
在这个“Struts2实现文件上传”的主题中,我们将深入探讨如何利用Struts2框架来实现在Web应用中的文件上传功能。 首先,我们注意到一个细节描述:“private String uploadContextType;应更正为private String ...
在Java Struts2框架中实现文件上传进度条显示,主要涉及到的技术点包括Struts2的文件上传、Ajax异步通信以及前端进度条组件的使用。下面将详细讲解这些知识点。 首先,Struts2的文件上传功能是通过Struts2提供的`...