1.upload.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@taglib uri="/struts-tags" prefix="s" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!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>Insert title here</title> <base href="<%=basePath%>"> </head> <body> <!-- <form action="upload" method="get"> 文件标题:<input type="text" name="filename"/><br/> 上传文件:<input type="file" name="file"><br/> <input type="submit" value="上传"/><br/> </form>--> <s:form action="upload1" enctype="multipart/form-data" methos="post"> <s:textfield name="title" label="文件名"></s:textfield><br/> <s:file name="upload" label="文件"></s:file><br/> <s:submit value="上传"></s:submit><br/> </s:form> </body> </html>
- jsp代码:
- <input type="file" id="license" name="license" style="opacity: 0;">
opacity: 0是让input type=”file”全透明,这样用户看不到input type=”file”。层级在文本框和按钮之上。这样用户在点击按钮的时侯实际上点击的input type=”file”;
使用js提交form表单时,給form定义id属性,直接$("#id").submit();不需要在有<intput type="submit"/>
2.struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <constant name="struts.i18n.encoding" value="utf-8"/> <package name="parameter" namespace="/" extends="struts-default"> <action name="upload1" class="com.ru.action.UploadAction"> <param name="savepath">/upload</param> <!-- 文件上传格式 --> <param name="allowtypes">image/gif,image/x-png,image/jpg,image/jpeg,application/vnd.openxmlformats-officedocument.wordprocessingml.document</param> <result name="sucess">/WEB-INF/jsp/sucess.jsp</result> <result name="error">/WEB-INF/jsp/error.jsp</result> </action> </package> </struts>
3.UploadAction.java
package com.ru.action; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport { //title和upload从jsp页面获取。 private String title; private File upload; //savePath设置文件的存储路径 private String savepath; //分别获取文件的名字和类型。只要有file类型的参数***就对应这两个属性 private String uploadFileName; private String uploadContentType; //文件过滤属性,通过struts.xml文件配置allowtypes属性值 private String allowtypes; public String getAllowtypes() { return allowtypes; } public void setAllowtypes(String allowtypes) { this.allowtypes = allowtypes; } public String getUploadContentType() { return uploadContentType; } public void setUploadContentType(String uploadContentType) { this.uploadContentType = uploadContentType; } public void setUploadFileName(String uploadFileName) { this.uploadFileName = uploadFileName; } public String getUploadFileName() { return uploadFileName; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public File getUpload() { return upload; } public void setUpload(File upload) { this.upload = upload; } public String getSavepath() throws Exception{ return ServletActionContext.getServletContext().getRealPath("/WEB-INF"+savepath); } public void setSavepath(String savepath) { this.savepath = savepath; } //过滤文件 public String filetypes(){ String filetype=getUploadContentType(); String[] types=getAllowtypes().split(","); for(String type:types){ if(type.equals(filetype)){ return "ok"; } } return "error"; } @Override public String execute() throws Exception { // TODO Auto-generated method stub //System.out.print(ServletActionContext.getServletContext().getRealPath("/WEB-INF")); if(filetypes().equals("error")){ this.addFieldError("uploadfileerror", "上传文件类型错误"); return "error"; } FileInputStream fis=new FileInputStream(getUpload()); byte buffer[]=new byte[1024]; int length=0; FileOutputStream fos=new FileOutputStream(getSavepath()+"\\"+getUploadFileName()); while((length=fis.read(buffer))>0){ fos.write(buffer, 0, length); } System.out.print(getUploadContentType()); return "sucess"; } }
相关推荐
在这个“Struts2实现文件上传”的主题中,我们将深入探讨如何利用Struts2框架来实现在Web应用中的文件上传功能。 首先,我们注意到一个细节描述:“private String uploadContextType;应更正为private String ...
Struts2是一个强大的MVC(模型-视图-控制器)框架,广泛应用于Java ...以上就是使用Struts2框架实现文件上传下载的基本步骤和关键知识点。在实际开发中,可以根据项目需求进行调整和优化,确保功能的稳定性和安全性。
下面将详细阐述如何使用Struts2来实现文件上传功能。 1. **Struts2文件上传组件** Struts2框架集成了一个名为`struts2-convention-plugin`的插件,它提供了文件上传的支持。主要依赖于`Commons FileUpload`和`...
在本文中,我们将深入探讨使用Struts2实现文件上传和下载的各种方案。 ### 文件上传方案 #### 1. 使用Struts2进行文件普通上传 文件上传的核心是处理`multipart/form-data`类型的表单数据。Struts2通过`Struts2-...
### Struts2实现文件上传(单个+多个文件上传) #### 一、单个文件上传 在Struts2框架中实现文件上传是一项常见的需求。本文将详细介绍如何在Struts2中实现单个文件的上传。 ##### JSP 页面设计 首先,我们需要在...
struts2 实现文件上传,手动对上传文件进行过滤,希望对大家有用 <param name="allowTypes">application/octet-stream,application/pdf</param> <!-- 仅允许上传ppt,pdf格式的 -->
在Struts2框架中,实现文件上传并展示进度条效果是一项常见的需求,尤其对于大型文件上传,用户界面的进度反馈能提供更好的用户体验。本文将详细讲解如何在Struts2中实现这一功能。 首先,我们需要理解Struts2处理...
### Struts2实现文件上传案例 #### 概述 本文将详细介绍如何利用Struts2框架实现文件上传功能。Struts2是Apache软件基金会的一个开源Web应用框架,它使用MVC设计模式并支持多种编程模式(如命令式、声明式、函数式...
在Struts2中实现文件上传,主要涉及到以下几个核心组件和步骤: 1. **配置Struts2**:首先,你需要在`struts.xml`配置文件中启用Struts2的文件上传支持。这通常通过添加`<constant>`标签并设置`struts.multipart....
以上是Struts2实现文件上传和下载的基本步骤和关键概念。在实际应用中,你可能还需要考虑性能优化、错误处理、用户体验等方面。阅读博文(https://chenzheng8975.iteye.com/blog/1733841)将为你提供更具体的实现...
在Java Struts2框架中实现文件上传进度条显示,主要涉及到的技术点包括Struts2的文件上传、Ajax异步通信以及前端进度条组件的使用。下面将详细讲解这些知识点。 首先,Struts2的文件上传功能是通过Struts2提供的`...
在Struts2中,文件上传主要依赖于`org.apache.struts2.components.FileUpload`组件,这个组件是基于Commons FileUpload库实现的,它能够处理multipart/form-data类型的HTTP请求,这是文件上传所必需的格式。...
【Uploadify + Struts2 实现文件上传详解】 在Web开发中,文件上传是一个常见的功能需求,尤其是在内容管理系统、论坛或者其他需要用户提交图片、文档等资料的场景。Uploadify是一款基于jQuery的文件上传插件,它...