jsp代码
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Struts2 File Upload</title>
</head>
<body>
<s:text name=""></s:text>
<form action="fileUpload.action" method="POST" enctype="multipart/form-data">
文件标题:<input type="text" name="title" size="50"/><br/>
选择文件:<input type="file" name="upload" size="50"/><br/>
选择文件:<input type="file" name="upload" size="50"/><br/>
选择文件:<input type="file" name="upload" size="50"/><br/>
<input type="submit" value=" 上传 "/>
</form>
</body>
</html>
baseAction文件
//pizza
package com.trendcom.upload.action;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class BaseAction extends ActionSupport {
public HttpServletRequest getRequest(){
return ServletActionContext.getRequest();
}
public HttpServletResponse getResponse(){
return ServletActionContext.getResponse();
}
public HttpSession getSession(){
return getRequest().getSession();
}
public ServletContext getServletContext(){
return ServletActionContext.getServletContext();
}
public String getRealyPath(String path){
return getServletContext().getRealPath(path);
}
}
action文件
package com.trendcom.upload.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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.ArrayList;
import java.util.List;
public class UploadFileAction extends BaseAction {
private static final int BUFFER_SIZE=16*1024;
// 文件标题
private String title;
// 用File数组来封装多个上传文件域对象
private File[] upload;
// 用String数组来封装多个上传文件名
private String[] uploadFileName;
// 用String数组来封装多个上传文件类型
private String[] uploadContentType;
// 保存文件的目录路径(通过依赖注入)
private String savePath;
//以下为所有属性的getter和setter。省略。。。
// 自己封装的一个把源文件对象复制成目标文件对象
private static boolean copy(File src, File dst) {
boolean result=false;
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];
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
result=true;
} catch (Exception e) {
e.printStackTrace();
result=false;
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
@Override
public String execute() throws Exception {
File[] srcFiles = this.getUpload();
List<String> successFileList=new ArrayList<String>();
// 处理每个要上传的文件
for (int i = 0; i < srcFiles.length; i++) {
// 根据服务器的文件保存地址和原文件名创建目录文件全路径
String dstPath = getRealyPath(getSavePath())
+ "\\" + this.getUploadFileName()[i];
File dstFile = new File(dstPath);
if(copy(srcFiles[i], dstFile)){
successFileList.add(getUploadFileName()[i]);
}
}
getRequest().setAttribute("successFileList", successFileList);
return SUCCESS;
}
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[] getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String[] getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}
配置文件
<?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="com.trendcom.upload.action" extends="struts-default">
<action name="fileUpload" class="com.trendcom.upload.action.UploadFileAction">
<interceptor-ref name="fileUpload">
<!-- 配置允许上传的文件类型,多个用","分隔 -->
<param name="allowedTypes">
image/bmp,image/png,image/gif,image/jpeg,image/jpg,image/x-png, image/pjpeg
</param>
<!-- 配置允许上传的文件大小,单位字节 -->
<param name="maximumSize">102400</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<!-- 动态设置Action中的savePath属性的值 -->
<param name="savePath">/upload</param>
<result name="input">/index.jsp</result>
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
分享到:
相关推荐
在Struts2中,文件上传和下载是常见的功能需求,特别是在处理用户交互和数据交换时。这篇博客文章提供的"struts2文件上传下载源代码"旨在帮助开发者理解和实现这些功能。 文件上传功能允许用户从他们的设备上传文件...
Struts2 文件上传是Web开发中的一个重要功能,它允许用户通过网页上传文件到服务器。Struts2 是一个基于MVC(Model-View-Controller)设计模式的Java Web框架,提供了丰富的特性和强大的控制层功能,使得文件上传...
这个压缩包包含了实现Struts2文件上传所需的全部jar包,这些库文件对于理解和实现文件上传功能至关重要。 首先,我们要了解Struts2文件上传的基本流程。当用户通过表单提交包含文件输入字段的请求时,Struts2框架会...
在Struts2中,文件上传功能是一个常见的需求,例如用户可能需要上传图片、文档或其他类型的文件。本教程将深入浅出地讲解如何在Struts2中实现文件上传,并提供一个简单的实例来帮助理解。 1. **Struts2文件上传概述...
Struts2文件上传是Web开发中常见的功能,用于允许用户在网页上选择并上传本地文件到服务器。在Java Web环境中,Struts2框架提供了一套完整的解决方案来处理文件上传请求。下面将详细介绍Struts2文件上传的核心概念、...
在Struts 2中,文件上传功能是通过使用Struts 2的插件机制来实现的,这使得开发者能够方便地处理用户上传的文件。下面将详细讨论Struts 2文件上传的相关知识点。 ### 1. Struts 2文件上传原理 文件上传是基于HTTP...
"Struts2文件上传带进度条页面无刷新"的实现涉及多个技术点,包括Struts2的文件上传插件、AJAX异步通信以及前端进度条展示。 首先,Struts2的文件上传依赖于`struts2-upload-plugin`。这个插件扩展了Struts2的核心...
在这个"Struts2之struts2文件上传详解案例struts011"中,我们将深入探讨如何实现这一功能。 首先,我们需要了解Struts2中的Action类,它是处理用户请求的核心组件。为了支持文件上传,我们需要创建一个继承自`org....