在Struts 2中实现文件上传
实现原理
具体实现
清单1 依赖类包的列表
<% @ taglib prefix = " s " uri = " /struts-tags " %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< title > Struts 2 File Upload </ title >
</ head >
< body >
< s:form action ="fileUpload" method ="POST" enctype ="multipart/form-data" >
< s:file name ="myFile" label ="Image File" />
< s:textfield name ="caption" label ="Caption" />
< s:submit />
</ s:form >
</ body >
</ html >
清单2 FileUpload.jsp
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport {
private static final long serialVersionUID = 572146812454l ;
private static final int BUFFER_SIZE = 16 * 1024 ;
private File myFile;
private String contentType;
private String fileName;
private String p_w_picpathFileName;
private String caption;
public void setMyFileContentType(String contentType) {
this .contentType = contentType;
}
public void setMyFileFileName(String fileName) {
this .fileName = fileName;
}
public void setMyFile(File myFile) {
this .myFile = myFile;
}
public String getImageFileName() {
return p_w_picpathFileName;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this .caption = caption;
}
private static void copy(File src, File dst) {
try {
InputStream in = null ;
OutputStream out = null ;
try {
in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
byte [] buffer = new byte [BUFFER_SIZE];
while (in.read(buffer) > 0 ) {
out.write(buffer);
}
} finally {
if ( null != in) {
in.close();
}
if ( null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static String getExtention(String fileName) {
int pos = fileName.lastIndexOf( " . " );
return fileName.substring(pos);
}
@Override
public String execute() {
p_w_picpathFileName = new Date().getTime() + getExtention(fileName);
File p_w_picpathFile = new File(ServletActionContext.getServletContext().getRealPath( " /UploadImages " ) + " / " + p_w_picpathFileName);
copy(myFile, p_w_picpathFile);
return SUCCESS;
}
}
清单3 tutorial/FileUploadAction.java
<% @ taglib prefix = " s " uri = " /struts-tags " %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< title > Struts 2 File Upload </ title >
</ head >
< body >
< div style ="padding: 3px; border: solid 1px #cccccc; text-align: center" >
< img src ='UploadImages/<s:property value ="p_w_picpathFileName" /> ' />
< br />
< s:property value ="caption" />
</ div >
</ body >
</ html >
清单4 ShowUpload.jsp
<! DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd" >
< struts >
< package name ="fileUploadDemo" extends ="struts-default" >
< action name ="fileUpload" class ="tutorial.FileUploadAction" >
< interceptor-ref name ="fileUploadStack" />
< result name ="success" > /ShowUpload.jsp </ result >
</ action >
</ package >
</ struts >
清单5 struts.xml
< web-app id ="WebApp_9" version ="2.4"
xmlns ="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee [url]http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd[/url]" >
< display-name > Struts 2 Fileupload </ display-name >
< filter >
< filter-name > struts-cleanup </ filter-name >
< filter-class >
org.apache.struts2.dispatcher.ActionContextCleanUp
</ filter-class >
</ filter >
< filter >
< filter-name > struts2 </ filter-name >
< filter-class >
org.apache.struts2.dispatcher.FilterDispatcher
</ filter-class >
</ filter >
< filter-mapping >
< filter-name > struts-cleanup </ filter-name >
< url-pattern > /* </ url-pattern >
</ filter-mapping >
< filter-mapping >
< filter-name > struts2 </ filter-name >
< url-pattern > /* </ url-pattern >
</ filter-mapping >
< welcome-file-list >
< welcome-file > index.html </ welcome-file >
</ welcome-file-list >
</ web-app >
清单6 WEB-INF/web.xml
清单7 FileUpload页面
清单8 上传成功页面
更多配置
INFO: Unable to find 'struts.multipart.saveDir' property setting. Defaulting to javax.servlet.context.tempdir
Mar 20 , 2007 4 : 08 : 43 PM org.apache.struts2.interceptor.FileUploadInterceptor intercept
INFO: Removing file myFile C:\Program Files\Tomcat 5.5 \work\Catalina\localhost\Struts2_Fileupload\upload_251447c2_1116e355841__7ff7_00000006.tmp
清单9 服务器控制台输出
清单10 struts配置
错误处理
< interceptor-ref name ="fileUpload" >
< param name ="allowedTypes" >
p_w_picpath/bmp,p_w_picpath/png,p_w_picpath/gif,p_w_picpath/jpeg
</ param >
</ interceptor-ref >
< interceptor-ref name ="defaultStack" />
< result name ="input" > /FileUpload.jsp </ result >
< result name ="success" > /ShowUpload.jsp </ result >
</ action >
清单11 修改后的配置文件
清单12 出错提示页面
清单13 自定义出错提示页面
多文件上传
< s:file label ="File (1)" name ="upload" />
< s:file label ="File (2)" name ="upload" />
< s:file label ="FIle (3)" name ="upload" />
< s:submit />
</ s:form >
清单14 多文件上传JSP代码片段
private String[] uploadFileNames;
private String[] uploadContentTypes;
public File[] getUpload() { return this .uploads; }
public void setUpload(File[] upload) { this .uploads = upload; }
public String[] getUploadFileName() { return this .uploadFileNames; }
public void setUploadFileName(String[] uploadFileName) { this .uploadFileNames = uploadFileName; }
public String[] getUploadContentType() { return this .uploadContentTypes; }
public void setUploadContentType(String[] uploadContentType) { this .uploadContentTypes = uploadContentType; }
清单15 多文件上传数组绑定Action代码片段
private List < String > uploadFileNames = new ArrayList < String > ();
private List < String > uploadContentTypes = new ArrayList < String > ();
public List < File > getUpload() {
return this .uploads;
}
public void setUpload(List < File > uploads) {
this .uploads = uploads;
}
public List < String > getUploadFileName() {
return this .uploadFileNames;
}
public void setUploadFileName(List < String > uploadFileNames) {
this .uploadFileNames = uploadFileNames;
}
public List < String > getUploadContentType() {
return this .uploadContentTypes;
}
public void setUploadContentType(List < String > contentTypes) {
this .uploadContentTypes = contentTypes;
}
清单16 多文件上传列表绑定Action代码片段
总结
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.6.jar
xwork-2.0.1.jar
commons-io-1.3.1.jar
commons-fileupload-1.2.jar
- <%@ page language="java" contentType="text/html; charset=GBK"%>
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <html>
- <head>
- <head>
- <body>
- <s:form action="fileUpload" method="post" enctype="multipart/form-data">
- <s:file name="doc" label="File"/>
- <s:submit/>
- <s:form>
- <body>
- <html>
- package fileupload;
- import java.io.File;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Random;
- import javax.servlet.ServletContext;
- import org.apache.commons.io.FileUtils;
- import org.apache.struts2.util.ServletContextAware;
- import com.opensymphony.xwork2.ActionSupport;
- public class FileUploadAction extends ActionSupport implements ServletContextAware {
- private static final long serialVersionUID = -5016873153441103539L;
- private File doc;
- private String fileName;
- private String contentType;
- private ServletContext context;
- public void setDoc(File file) {
- this.doc = file;
- }
- public void setDocFileName(String fileName) {
- this.fileName = fileName;
- }
- public void setDocContentType(String contentType) {
- this.contentType = contentType;
- }
- public void setServletContext(ServletContext context) {
- this.context = context;
- }
- public String execute() throws Exception {
- String targetDirectory = context.getRealPath("/upload");
- String targetFileName = generateFileName(fileName);
- File target = new File(targetDirectory, targetFileName);
- FileUtils.copyFile(doc, target);
- return SUCCESS;
- }
- private String generateFileName(String fileName) {
- DateFormat format = new SimpleDateFormat("yyMMddHHmmss");
- String formatDate = format.format(new Date());
- int random = new Random().nextInt(10000);
- int position = fileName.lastIndexOf(".");
- String extension = fileName.substring(position);
- return formatDate + random + extension;
- }
- }
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <package name="fileupload" namespace="/fileupload" extends="struts-default">
- <action name="fileUpload" class="fileupload.FileUploadAction">
- <result>/fileupload/upload_success.jsp</result>
- </action>
- </package>
- </struts>
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.4"
- xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
- [url]http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd[/url]">
- <filter>
- <filter-name>struts-cleanup</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
- </filter>
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts-cleanup</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- </web-app>
转载于:https://blog.51cto.com/su3390/39277
在Struts 2中实现文件上传相关推荐
- 在Struts2中实现文件上传(二)
发布运行应用程序,在浏览器地址栏中键入:http://localhost:8080/Struts2_Fileupload/FileUpload.jsp,出现图示页面: 清单7 FileUpload ...
- IIS 6和IIS 7 中设置文件上传大小限制设置方法,两者是不一样的
在IIS 6.0中设置文件上传大小的方法,只要设置httpRuntime就可以了 <system.web><httpRuntime executionTimeout="36 ...
- java语言 文件上传,java中实现文件上传的方法
java中实现文件上传的方法 发布时间:2020-06-19 10:29:11 来源:亿速云 阅读:86 作者:Leah 这篇文章给大家分享的是java中实现文件上传的方法,相信大部分人都还没学会这个 ...
- Asp.net MVC 1.0 RTM中实现文件上传
Asp.net MVC 1.0 RTM中实现文件上传 在我们开始之前,你需要知道一个form以post方式上传文件的方式,你将要增加一个特别的enctype attribute到form标签上,为了这 ...
- jsp servlet示例_Servlet和JSP中的文件上传示例
jsp servlet示例 使用Servlet和JSP将文件上传到服务器是Java Web应用程序中的常见任务. 在对Servlet或JSP进行编码以处理文件上传请求之前,您需要了解一点有关HTML和 ...
- Servlet和JSP中的文件上传示例
使用Servlet和JSP将文件上传到服务器是Java Web应用程序中的常见任务. 在对Servlet或JSP进行编码以处理文件上传请求之前,您需要了解一点有关HTML和HTTP协议中文件上传支持的 ...
- tomcat temp 大量 upload 文件_问题:JavaWeb中实现文件上传的方式有哪些?
问题:JavaWeb中实现文件上传的方式有哪些? 上回我们说了下文件下载的方式有哪些,这次我们从不同的环境下简单来说说文件上传的方式有哪些. 文件上传的方式 Servlet2.5 方式 Servlet ...
- (转)SpringMVC学习(九)——SpringMVC中实现文件上传
http://blog.csdn.net/yerenyuan_pku/article/details/72511975 这一篇博文主要来总结下SpringMVC中实现文件上传的步骤.但这里我只讲单个文 ...
- 在WebBrowser中通过模拟键盘鼠标操控网页中的文件上传控件
引言 这两天沉迷了Google SketchUp,刚刚玩够,一时兴起,研究了一下WebBrowser. 我在<WebBrowser控件使用技巧分享>一文中曾谈到过"我现在可以通过 ...
最新文章
- Java项目:化妆品商城系统(java+Springboot+ssm+mysql+jsp+maven)
- python爬虫如何从一个页面进入另一个页面-Python爬虫信息输入及页面的切换方法...
- javax.servlet.GenericServlet类(协议无关版本)
- 循序渐进Java Socket网络编程(多客户端、信息共享、文件传输)
- Makefile的call函数
- 高德地图android4,Android高德之旅(4)我的位置
- left edge algorithm.
- Java并发编程的基础-interrupt方法
- shell学习(12)- jq
- python人脸识别门禁_用Python做人脸识别
- 历时7周,当然了真正的安装时间没这么多,终于把集群安装好了
- C - Swaps 2(树状数组,思维)
- 【转】DICOM文件格式剖析(初识)
- 9203-1204-抄写
- Qt Dialog捕捉鼠标移动事件
- linux6.3 dhcp,51CTO博客-专业IT技术博客创作平台-技术成就梦想
- 从汇编的眼光看C++(之delete内存泄露)
- 四、mysql数据常用命令
- 信创产业已成现象级新风口 代码“源头”安全该如何守护?
- 关于python中enumerate和zip函数的用法及举例