1首先写前台的现实上传文件的界面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<div>
<div>
<h4>单文件上传</h4>
<s:form action="doUpload" method="post" enctype="multipart/form-data">
<!-- 注意这里form表单的后面的method,和enctype是固定的写法,写过了就上传不成功要细心-->
<s:file name="upload" label="File"/>
<s:submit/>
</s:form>
</div>
<s:fielderror></s:fielderror>
</body>
</html>
2,写后台的action类,处理前面表单发过来的数据
package cn.huas.struts2.fileupload;
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 UploadAction extends ActionSupport
{
private File file;
private String contentType;
private String filename;
private static final int BUFFER_SIZE = 16 * 1024 ;
//这里的setUpload中的Upload要与表单中的file便签的name属性相同,而且首字母大写,再再前面加set,与javabean的写法类似
public void setUpload(File file) {
this.file = file;
}
public void setUploadContentType(String contentType) {
this.contentType = contentType;
}
public void setUploadFileName(String filename) {
this.filename = filename;
}
public String execute() {
//...
System.out.println("file name:"+this.filename);
System.out.println("Content Type:"+this.contentType);
String imageFileName = new Date() + getExtention(this.filename);
File imageFile = new File(ServletActionContext.getServletContext().getRealPath("/" )+"upload/" + filename);
copy(file, imageFile);
System.out.println(imageFile.getAbsolutePath());
System.out.println("copy success!");
return SUCCESS;
}
//取得文件名的文件类型
private static String getExtention(String fileName) {
int pos = fileName.lastIndexOf(" . ");
if(pos!=-1)
return fileName.substring(pos);
else
return Math.random()*10000+"";
}
//将临时文件保存到服务器的某个目录
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();
}
}
}
3.配置struts.xml文件
<?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="cn.huas.struts2.fileupload" extends="struts-default">
<action name="doUpload" class="cn.huas.struts2.fileupload.UploadAction">
<interceptor-ref name="defaultStack">
<!-- 配置允许上传的文件类型,多个用","分隔 -->
<param name="fileUpload.allowedTypes">
image/bmp,image/png,image/gif,image/jpeg,image/jpg
,image/x-png, image/pjpeg ,text/plain
</param>
<!-- 配置允许上传的文件大小,单位字节,本例为:1MB -->
<param name="fileUpload.maximumSize">1048576</param>
</interceptor-ref>
<result name="success">success.jsp</result>
<result name="input">index.jsp</result>
</action>
</package>
</struts>
4,最后就可以在浏览器里面输入项目的地址访问啦。
分享到:
相关推荐
在这个“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的文件上传插件,它...