struts2之文件上传
一、单文件上传
实例:
表单应该注意三个点 form中的method="post"、enctype="multipart/form-data"、input中的type="file"
fileForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>File Operation</title>
</head>
<body>
<form action="file/fileOperation_upload" method="post"
enctype="multipart/form-data">
<label for="myFile">Upload your file</label><br />
<input type="file" name="myFile">
<input type="submit" value="上传">
</form>
</body>
</html>
struts.xml中配置文件
<package name="file" namespace="/file" extends="struts-default">
<action name="fileOperation_*" class="com.lz.web.action.FileOperation" method="{1}" >
<result name="upload">/WEB-INF/pages/file.jsp</result>
<result name="error">/WEB-INF/pages/error.jsp</result>
</action>
</package>
FileOperationAction.java
package com.lz.web.action; import java.io.File;
import java.io.IOException; import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial")
public class FileOperation extends ActionSupport{ private File myFile;
private String myFileContentType;
private String myFileFileName;
private String destPath; public File getMyFile() {
return myFile;
} public void setMyFile(File myFile) {
this.myFile = myFile;
} public String getMyFileContentType() {
return myFileContentType;
} public void setMyFileContentType(String myFileContentType) {
this.myFileContentType = myFileContentType;
} public String getMyFileFileName() {
return myFileFileName;
} public void setMyFileFileName(String myFileFileName) {
this.myFileFileName = myFileFileName;
} public String upload(){ destPath= ServletActionContext.getServletContext().getRealPath("/files");//文件保存的路径
System.out.println("realpath: "+destPath);
System.out.println("Src filename: "+myFile);
System.out.println("Dest filename: "+myFileFileName);
try {
File destFile = new File(destPath, myFileFileName);
FileUtils.copyFile(myFile, destFile);//拷贝文件
} catch (IOException e) {
e.printStackTrace();
return "error";
} return "upload";
}
public String prefile(){ return "prefile";
}
}
file.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!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>
</head>
<body>
your have successfully uploaded <s:property value="myFileFileName"/>
</body>
</html>
二、多文件上传
fileForm2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>File Operation</title>
</head>
<body>
<form action="file/multiFileUploadAction_upload" method="post"
enctype="multipart/form-data">
文件1:<input type="file" name="image"><br/>
文件2:<input type="file" name="image"><br/>
文件3:<input type="file" name="image"><br/>
<input type="submit" value="上传">
</form>
</body>
</html>
struts.xml中配置
<package name="mfile" namespace="/file" extends="struts-default">
<action name="multiFileUploadAction_*" class="com.lz.web.action.MultiFileUploadAction" method="{1}">
<result name="load">/WEB-INF/pages/fileForm2.jsp</result>
<result name="upload2">/WEB-INF/pages/file2.jsp</result>
</action>
</package>
MultiFileUploadAction.java
package com.lz.web.action; import java.io.File;
import java.io.IOException; import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial")
public class MultiFileUploadAction extends ActionSupport{ private File[] image;
private String[] imageFileName;
private String[] imageContentType; public File[] getImage() {
return image;
}
public void setImage(File[] image) {
this.image = image;
}
public String[] getImageFileName() {
return imageFileName;
}
public void setImageFileName(String[] imageFileName) {
this.imageFileName = imageFileName;
}
public String[] getImageContentType() {
return imageContentType;
}
public void setImageContentType(String[] imageContentType) {
this.imageContentType = imageContentType;
}
public String load(){
return "load";
}
public String upload(){ String realPath = ServletActionContext.getServletContext().getRealPath("/files");//指定文件保存的路径
System.out.println("realpath: "+realPath);
try {
if(image!=null){
File savedir = new File(realPath);
if(!savedir.getParentFile().exists())//判断文件目录是否存在
savedir.getParentFile().mkdirs();
for(int i=0; i<image.length; i++){
File saveFile = new File(realPath, imageFileName[i]);
FileUtils.copyFile(image[i], saveFile);//文件复制
}
}
} catch (IOException e) {
e.printStackTrace();
}
return "upload2";
} }
file2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!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>
</head>
<body>
上传成功<br/>
<s:iterator value="imageFileName" var="ifn">
<s:property value="ifn"/>
</s:iterator>
</body>
</html>
struts2之文件上传的更多相关文章
- struts2的文件上传
在做B/S系统时,通常会涉及到上传文件和下载文件,在没接struts2框架之前,我们都是使用apache下面的commons子项目的FileUpload组件来进行文件的上传,但是那样做的话,代码看起来 ...
- jsp\struts1.2\struts2 中文件上传(转)
jsp\struts1.2\struts2 中文件上传 a.在jsp中简单利用Commons-fileupload组件实现 b.在struts1.2中实现c.在sturts2中实现现在把Code与大家 ...
- Struts2+Uploadify文件上传使用详解
Uploadify是JQuery的一个上传插件,实现的效果非常不错,带进度显示.不过官方提供的实例是php版本的,本文将详细介绍Uploadify在java中的使用,您也可以点击下面的链接进行演示或下 ...
- Struts2 多文件上传
Struts2多文件上传只需要将 单文件上传中的File变成File[] 即可,上篇文章:单文件上传 <form action="${pageContext.request.cont ...
- Struts2图片文件上传,判断图片格式和图片大小
1. 配置Struts2能够上传的最大文件大小 使用Struts2进行文件上传的时候,Struts2默认文件大小最大为2MB,如果要传大一点的文件,就需要修改struts.xml配置文件,重新设置能够 ...
- Struts2实现文件上传下载功能(批量上传)
今天来发布一个使用Struts2上传下载的项目, struts2为文件上传下载提供了好的实现机制, 首先,可以先看一下我的项目截图 关于需要使用的jar包,需要用到commons-fileupload ...
- Struts2实现文件上传(四)
Struts2实现文件上传 配置文件struts.xml <!-- /* * $Id: struts.xml 1364077 2012-07-21 12:57:02Z lukaszlenart ...
- Struts2实现文件上传(三)
Struts2实现文件上传 配置文件web.xml <?xml version="1.0" encoding="UTF-8"?> <web-a ...
- Struts2实现文件上传(二)
Struts2实现文件上传 文件上传页面 file.jsp: <%@ page language="java" import="java.util.*" ...
- Struts2实现文件上传(一)
Struts2实现文件上传 文件上传成功后结果页面 result.jsp: <%@ page language="java" import="java.util.* ...
随机推荐
- linux环境下nginx配置
1.反向代理配置 # nginx/conf/nginx.conf
- Docker学习之镜像操作
使用Docker镜像 以下都是Docker镜像的一系列重要名操作,包括获取.查看.搜索.删除.创建.存出或载入.上传等.可使用docker image help命令查看帮助. 1.获取镜像(pull) ...
- Unity基础
unity unity 3大场景 Asset Scene Component Asset :资源导入导出 右击资源,选择导出Unity包 导入可以直接将只有复制到Asset文件夹 创建场景 File- ...
- (转)为什么国外 MMORPG 中不采用自动寻路等功能?
不只是自动寻路,现在网游中的教学引导系统,辅助系统的功能强大程度,友好程度都可以说到了变态的程度,开发这些功能投入的资源甚至要超过游戏内容本身.究其原因,还是竞争越来越激烈,人心越来越浮躁,游戏商家为 ...
- Linux编译移植Qt5的环境_Xillinx的ZYNQ平台
Linux编译Qt环境 2017年的十一假期,足不出户,一个人在教研室里面搞Qt的移植.我手里面有Samsung的CortexA8,Samsung的 CortexA53还有Ti的Sitara系列的AM ...
- 各种Nand的总结
1. 微观 NAND闪存NAND是非易失性存储技术,NAND闪存由多个存放以位(bit)为单位的单元构成,这些位通过电荷被打开或关闭,如何组织这些开关单元来储存在SSD上的数据,也决定了NAND闪存的 ...
- Leetcode 173. 二叉搜索树迭代器
题目链接 https://leetcode.com/problems/binary-search-tree-iterator/description/ 题目描述 实现一个二叉搜索树迭代器.你将使用二叉 ...
- 设置默认以管理员运行的WinForm
右键工程名, 属性; 选择"安全性"; 勾选"启用ClickOnce安全设置"与"这是完全可信的应用程序"; 退出该页面, app.mani ...
- iOS算法笔记-快速排序-OC实现
快速排序(Quicksort)是对冒泡排序的一种改进. 快速排序由C. A. R. Hoare在1962年提出.它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另 ...
- python实现单链表的反转
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #!/usr/bin/env python #coding = utf-8 ...