struts2实现图片上传功能
2017-02-17 17:02
267 查看
第一步jar包:maven pom.xml中的坐标如下
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>
文件上传的需要的jar
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
第二步:先写前端页面如
web-inf下的**.jsp
<script type="text/javascript">
$(function(){
$('#image').on('click',imageUpload);
});
function imageUpload(){
$("[name='image']").click();
upload();
}
function fileUpload(){
}
function upload(){
$('form').submit();
}
</script>
</head>
<body>
<h1>欢迎来到主页...这里实现的功能将是上传照片</h1>
<div>
<form action="upload.action" method="post" enctype="multipart/form-data" >
<input type="hidden" value="${imageFileName}" id="fileName">
<input type="file" name="image" style="display:none;" onclick="fileUpload();"/>
</form>
<div style="padding-top: 10px;float: right;">
<img id="image" name="" src="${pageContext.request.contextPath}/image/${imageFileName}" width="100px" height="100px"/>
</div>
</body>
第三步配置struts2.xml
<package name="myPackage" extends="struts-default">
<action name="upload" class="ssh.zzx.action.FileUploadAction" >
<interceptor-ref name="fileUpload">
<!-- 限制文件的类型 -->
<param name="allowedTypes">
image/bmp,image/png,image/gif,image/jpeg,image/jpg
</param>
<!-- 限制文件的大小 -->
<param name="maximumSize">10M</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result>/WEB-INF/jsp/index.jsp</result>
<result name="input">/error.jsp</result>
</action>
<package>
接下来就是ssh.zzx.action.FileUploadAction 这个Action类了
package ssh.zzx.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@Namespace("/")
public class FileUploadAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
*
*/
private File image;// 得到上传的文件
private String imageFileName;// 得到上传文件的名称
private String imageContentType;// 得到上传文件的类型
private Map<String,Object> session;
public String execute() throws IOException {
if(image==null){
return "input";
}
session=ActionContext.getContext().getSession();
// 根据上下文获得根目录
String path = ServletActionContext.getServletContext().getRealPath(
"/image");
// 用文件输入流读取文件
InputStream is = new FileInputStream(image);
// 判断文件存不存在
File file = new File(path, imageFileName);
if (!file.exists()) {
file.createNewFile();
}
// 用文件输出流创建一个文件,这里创建的文件名为imageFileName
OutputStream os = new FileOutputStream(file);
// 按字节输出
byte[] buffer = new byte[500];
int length = 0;
while (-1 != (length = is.read(buffer, 0, buffer.length))) {
os.write(buffer);
}
session.put("imageFileName", imageFileName);
os.close();
is.close();
return SUCCESS;
}
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 Map<String, Object> getSession() {
return session;
}
public void setSession(Map<String, Object> session) {
this.session = session;
}
}
到这里就ok了!!!
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>
文件上传的需要的jar
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
第二步:先写前端页面如
web-inf下的**.jsp
<script type="text/javascript">
$(function(){
$('#image').on('click',imageUpload);
});
function imageUpload(){
$("[name='image']").click();
upload();
}
function fileUpload(){
}
function upload(){
$('form').submit();
}
</script>
</head>
<body>
<h1>欢迎来到主页...这里实现的功能将是上传照片</h1>
<div>
<form action="upload.action" method="post" enctype="multipart/form-data" >
<input type="hidden" value="${imageFileName}" id="fileName">
<input type="file" name="image" style="display:none;" onclick="fileUpload();"/>
</form>
<div style="padding-top: 10px;float: right;">
<img id="image" name="" src="${pageContext.request.contextPath}/image/${imageFileName}" width="100px" height="100px"/>
</div>
</body>
第三步配置struts2.xml
<package name="myPackage" extends="struts-default">
<action name="upload" class="ssh.zzx.action.FileUploadAction" >
<interceptor-ref name="fileUpload">
<!-- 限制文件的类型 -->
<param name="allowedTypes">
image/bmp,image/png,image/gif,image/jpeg,image/jpg
</param>
<!-- 限制文件的大小 -->
<param name="maximumSize">10M</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result>/WEB-INF/jsp/index.jsp</result>
<result name="input">/error.jsp</result>
</action>
<package>
接下来就是ssh.zzx.action.FileUploadAction 这个Action类了
package ssh.zzx.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@Namespace("/")
public class FileUploadAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
*
*/
private File image;// 得到上传的文件
private String imageFileName;// 得到上传文件的名称
private String imageContentType;// 得到上传文件的类型
private Map<String,Object> session;
public String execute() throws IOException {
if(image==null){
return "input";
}
session=ActionContext.getContext().getSession();
// 根据上下文获得根目录
String path = ServletActionContext.getServletContext().getRealPath(
"/image");
// 用文件输入流读取文件
InputStream is = new FileInputStream(image);
// 判断文件存不存在
File file = new File(path, imageFileName);
if (!file.exists()) {
file.createNewFile();
}
// 用文件输出流创建一个文件,这里创建的文件名为imageFileName
OutputStream os = new FileOutputStream(file);
// 按字节输出
byte[] buffer = new byte[500];
int length = 0;
while (-1 != (length = is.read(buffer, 0, buffer.length))) {
os.write(buffer);
}
session.put("imageFileName", imageFileName);
os.close();
is.close();
return SUCCESS;
}
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 Map<String, Object> getSession() {
return session;
}
public void setSession(Map<String, Object> session) {
this.session = session;
}
}
到这里就ok了!!!
相关文章推荐
- Struts2实现图片上传功能 (单个图片文件)
- form与Struts2实现图片上传功能
- struts2 图片上传至服务器 页面预览及页面及时图片预览功能实现方式
- Java框架Struts2实现图片上传功能
- Struts2实现图片上传功能
- struts2上传图片功能实现(单个图片)
- Struts2实现图片上传功能
- struts2实现图片的上传以及下载功能
- swfupload+json+struts2实现多图片上传功能(一)
- 在asp.Net中使用fileuplod控件实现上传图片的功能
- Spring MVC 实现图片上传功能
- 【转】用Struts2实现文件上传时显示进度条功能
- jsp实现图片上传功能
- 通过JS实现上传图片的功能
- 上传图片功能的又一实现
- 一个实现图片上传/产生缩略图/在上传图片上写字功能的完整页面代码
- 用Struts2更好的实现文件的上传、下载功能以及解决中文名称问题
- struts2实现上传文件、生成缩略图、添加文字和图片水印
- 【SSI开发总结.8】Struts2中实现文件上传功能
- [Silverlight学习笔记]实现上传图片功能时遇到的问题