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. nginx搭建笔记

    1. nginx安装 Env: Mac OS 10.10 Yosemite pcre: http://pcre.org/ $tar -zxf pcre-8.34.tar.gz $cd pcre-8.3 ...

  2. java JFrame修改左上角的图片

    直接案例:

  3. elasticsearch-查询

    使用如下语句创建一个名字为:user_document,别名为user的索引 PUT:http://localhost:9200/user_document { "settings" ...

  4. mysql下载安装

    1.下载 下载地址:http://dev.mysql.com/downloads/mysql/ 根据电脑配置来选,我选了 windows(x86,64-bit),ZIP Archive这个,点击Dow ...

  5. matlab资源

    百度网盘  链接:http://pan.baidu.com/s/1c06ikEW 密码:9dpt包含matlab6.5,7,7.01,7.04,7.1,Matlab2006b(7.3),Matlab  ...

  6. DIY操作系统(一)

    先说几句题外话: 回想第一次看到<30天自制操作系统>这本书时,就被这快餐般的标题深深吸引了,我无法想象如此复杂有内涵的内容能在30天就弄出来,直到我花了一个多月看到这本书的第9天时,我放 ...

  7. oracle中imp命令详解 .

    转自http://www.cnblogs.com/songdavid/articles/2435439.html oracle中imp命令详解 Oracle的导入实用程序(Import utility ...

  8. ubuntu安装octave的小坑

    出现了以下情况: After this operation, 163 MB of additional disk space will be used.Do you want to continue? ...

  9. 使用Excel对象模型在Excel单元格中设置不同的字体

    效果是这样的: 首先找到这个单元格或区域Range cell,然后代码: ((Range)cell). Characters[, ].Font.Color = Color.Blue; ((Range) ...

  10. halcon三种模板匹配方法

    halcon有三种模板匹配方法:即Component-Based.Gray-Value-Based.Shaped_based,分别是基于组件(或成分.元素)的匹配,基于灰度值的匹配和基于形状的匹配,此 ...