Struts2的上传
1、Struts2默认采用了apache commons-fileupload
2、Struts2支持三种类型的上传组件
3、需要引入commons-fileupload相关依赖包
* commons-io-1.3.2.jar
* commons-fileupload-1.2.1.jar
4、表单中需要采用POST提交方式,编码类型需要使用:multipart/form-data
5、Struts2的Action
取得文件名称->>规则:输入域的名称+固定字符串FileName
取得文件数据->>规则:File 输入域的名称
取得内容类型->>规则:输入域的名称+固定字符串ContentType
6、得到输入流,采用输出流写文件
 
Action类
package com.djoker.struts2;

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 org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.Action; public class uploadAction { private String myFileFileName; private File myFile; private String descContextType; public String getMyFileFileName() {
return myFileFileName;
} public void setMyFileFileName(String myFileFileName) {
this.myFileFileName = myFileFileName;
} public File getMyFile() {
return myFile;
} public void setMyFile(File myFile) {
this.myFile = myFile;
} public String getDescContextType() {
return descContextType;
} public void setDescContextType(String descContextType) {
this.descContextType = descContextType;
} public String execute() throws Exception {
System.out.println(myFileFileName);
InputStream is = null;
OutputStream os = null;
try{
is = new BufferedInputStream(new FileInputStream(myFile));
os = new BufferedOutputStream(new FileOutputStream(ServletActionContext.getServletContext().getRealPath("upload") + "/" + myFileFileName));
byte[] ByteBuffer = new byte[1024];
int len = 0;
while((len = is.read(ByteBuffer)) > 0){
os.write(ByteBuffer, 0, len);
}
} finally {
if(is != null){
is.close();
}
if(os != null){
os.close();
}
}
return Action.SUCCESS;
}
}
upload.jsp文件
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<form action="uploadAction.action" method="post" enctype="multipart/form-data">
选择文件:<input type="file" name="myFile"><br>
文件描述:<input type="text" name="desc"><br>
<input type="submit" value="上传">
</form>
</body>
</html>
 
 
struts.xml配置中,配置最大文件上传限制
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <!-- 当struts.xml配置文件发生修改,会立刻加载,在生产环境下最好不要配置 -->
<constant name="struts.configuration.xml.reload" value="true"></constant>
<!-- 提供更加友好的提示信息 -->
<constant name="struts.devMode" value="true"></constant>
<!-- 对字符集的设置 -->
<constant name="struts.i18n.encoding" value="GB18030"/>
<!-- 配置文件上传最大限制 -->
<constant name="struts.multipart.maxSize" value="9999999999"></constant>
<package name="struts2" extends="struts-default" >
<global-results>
<result>/success.jsp</result>
<result name="error">/error.jsp</result>
</global-results>
<action name="login" class="com.djoker.struts2.LoginAction">
<result>/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
<action name="uploadAction" class="com.djoker.struts2.uploadAction">
<result>/success.jsp</result>
</action>
</package> <include file="struts-user.xml"></include>
</struts>

struts2学习笔记之十:文件上传的更多相关文章

  1. [原创]java WEB学习笔记49:文件上传基础,基于表单的文件上传,使用fileuoload 组件

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  2. PHP学习笔记--文件目录操作(文件上传实例)

    文件操作是每个语言必须有的,不仅仅局限于PHP,这里我们就仅用PHP进行讲解 php的文件高级操作和文件上传实例我放在文章的最后部分.--以后我还会给大家写一个PHP类似于网盘操作的例子 注意:阅读此 ...

  3. Struts2学习(六)———— 文件上传和下载

    一.单文件上传 在没学struts2之前,我们要写文件上传,非常麻烦,需要手动一步步去获取表单中的各种属性,然后在进行相应的处理,而在struts2中就不需要了,因为有一个fileUpload拦截器帮 ...

  4. (转载)JavaWeb学习总结(五十)——文件上传和下载

    源地址:http://www.cnblogs.com/xdp-gacl/p/4200090.html 在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传 ...

  5. JavaWeb学习总结(五十)——文件上传和下载

    在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...

  6. PHP学习笔记 02 之文件上传

    我们了解了表单传值后,这些我就可以完成PHP的文件上传了.我们了解PHP文件上传前,先了解PHP文件上传的原理. 一.PHP上传文件原理 第一步:将本地的文件通过form表单上传到服务器的临时目录中, ...

  7. Kali学习笔记38:文件上传漏洞

    早些年,提到Web渗透,或者搜索一些黑客教程 基本都会看到文件上传漏洞. 它是一个很经典的漏洞 但它本质其实不是一个漏洞,而是网站本身的上传文件功能 不过如果我们上传了Webshell,那么就成为了文 ...

  8. SpringMVC学习笔记八:文件上传下载(转)

    转自:http://www.cnblogs.com/WJ-163/p/6269409.html 一.关键步骤 ①引入核心JAR文件 SpringMVC实现文件上传,需要再添加两个jar包.一个是文件上 ...

  9. SpringBoot学习笔记(8)-----SpringBoot文件上传

    直接上代码,上传文件的前端页面: <body> <form action="/index/upload" enctype="multipart/form ...

随机推荐

  1. android 模拟器上网问题

    android 模拟器上网问题 1.配置Adroid环境变量(Win7为例) ,启动模拟器 第一步:桌面右键——>我的电脑——>高级系统设置    第二步:高级——>环境变量——&g ...

  2. 扩大a标签的响应区域

    <a href="" style="display:inline-block; width: 100%;">xx</a>

  3. js基础知识点总结

    如何在一个网站或者一个页面,去书写你的js代码:1.js的分层(功能):jquery(tool) 组件(ui) 应用(app),mvc(backboneJs)2.js的规划():避免全局变量和方法(命 ...

  4. CodeForces 742A Arpa’s hard exam and Mehrdad’s naive cheat

    题意:求1378 n次幂的最后一位. 析:两种方法,第一种,就是快速幂,第二种找循环节,也很好找,求一下前几个数就好. 代码如下: #pragma comment(linker, "/STA ...

  5. Java学习笔记 03 数组

    一.数组的创建和使用 数组的创建和使用 >>创建方法1:先声明,再用new关键字分配内存(使用new关键字分配内存,整形数组中各个元素的初始值都为0) String str[]; str= ...

  6. VS2015调试UWP程序时提示错误DEP0700 : Registration of the app failed. Another user has already installed

    在同一台windows10电脑上调试过一个工程以后,切换了账号再次调试出现错误 DEP0700 : Registration of the app failed. Another user has a ...

  7. 使用IConfigurationSectionHandler在web.config中增加自定义配置

    一. 场景    这里仅举一个简单应用的例子,我希望在web.config里面增加网站的基本信息,如:网站名称,网站版本号,是否将网站暂时关闭等.二. 基本实现方法1. 定义配置节点对应的类:Site ...

  8. System.Web.Mvc.UrlHelper的学习与使用

    我们学习一下UrlHelper帮助类,看类名也都知道这个类是用来帮我们生成URL在ASP.NET MVC应用程序中.让我们来看看该类给我们带来了哪些方便的方法和属性,UrlHelper提供了四个非常常 ...

  9. chrome浏览器root用户运行

    vim /usr/bin/google-chrome 58 exec -a "$0" "$HERE/chrome" "$PROFILE_DIRECTO ...

  10. Linux内核--网络栈实现分析(五)--传输层之UDP协议(上)

    本文分析基于Linux Kernel 1.2.13 原创作品,转载请标明出处http://blog.csdn.net/yming0221/article/details/7532512 更多请看专栏, ...