1、新建upload.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head> <body>
<s:form action="upload" theme="simple" method="post" namespace="" enctype="multipart/form-data">
<table align="center" width="40%" border="1">
<tr>
<td>
username
</td>
<td>
<s:textfield name="username" ></s:textfield>
</td>
</tr>
<tr>
<td>
password
</td>
<td>
<s:textfield name="password" ></s:textfield>
</td>
</tr>
<tr>
<td>
file
</td>
<td>
<s:file name="file"></s:file>
</td>
</tr>
<tr>
<td>
<s:submit value=" submit "></s:submit>
</td>
<td>
<s:reset value=" reset "></s:reset>
</td>
</tr>
</table> </s:form>
</body>
</html>

2、新建uploadResult.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'uploadResult.jsp' starting page</title>
</head> <body>
username:<s:property value="username"></s:property>
password:<s:property value="password"></s:property>
file:<s:property value="fileFileName"></s:property>
</body>
</html>

3、新建UploadAction.java

package com.test.action;

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.ActionSupport; public class UploadAction extends ActionSupport { private static final long serialVersionUID = 1L; private String username; private String password;
//必须添加的三个属性
  //file
private File file;
//fileFileName
private String fileFileName;
//fileContentType
private String fileContentType; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public File getFile() {
return file;
} public void setFile(File file) {
this.file = file;
} public String getFileFileName() {
return fileFileName;
} public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
} public String getFileContentType() {
return fileContentType;
} public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
} public String execute() throws Exception
{
InputStream is = new FileInputStream(file);
String root = ServletActionContext.getServletContext().getRealPath("/upload");
File destFile = new File(root,this.getFileFileName());
OutputStream os = new FileOutputStream(destFile);
//字节数组
byte[] buffer = new byte[400];
int length = 0;
while ((length = is.read(buffer))>0) {
os.write(buffer);
}
is.close();
os.close();
return SUCCESS;
}
}

4、struts.xml

            <action name="upload" class="com.test.action.UploadAction">
<result name="success">/uploadResult.jsp</result>
</action>

多文件上传:

1、upload.jsp

  <body>
<s:form action="upload" theme="simple" method="post" namespace="" enctype="multipart/form-data">
<table align="center" width="40%" border="1">
<tr>
<td>
username
</td>
<td>
<s:textfield name="username" ></s:textfield>
</td>
</tr>
<tr>
<td>
password
</td>
<td>
<s:textfield name="password" ></s:textfield>
</td>
</tr>
<tr>
<td>
file1
</td>
<td>
<s:file name="file"></s:file>
</td>
</tr>
<tr>
<td>
file2
</td>
<td>
<s:file name="file"></s:file>
</td>
</tr>
<tr>
<td>
file3
</td>
<td>
<s:file name="file"></s:file>
</td>
</tr>
<tr>
<td>
<s:submit value=" submit "></s:submit>
</td>
<td>
<s:reset value=" reset "></s:reset>
</td>
</tr>
</table> </s:form>
</body>

2、uploadResult.jsp

  <body>
username:<s:property value="username"></s:property>
password:<s:property value="password"></s:property>
file:<s:property value="fileFileName"></s:property>
</body>

3、uploadAction.jsp

package com.test.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List; import javax.swing.ListModel; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport { private static final long serialVersionUID = 1L; private String username; private String password; private List<File> file; private List<String> fileFileName; private List<String> fileContentType; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public List<File> getFile() {
return file;
} public void setFile(List<File> file) {
this.file = file;
} public List<String> getFileFileName() {
return fileFileName;
} public void setFileFileName(List<String> fileFileName) {
this.fileFileName = fileFileName;
} public List<String> getFileContentType() {
return fileContentType;
} public void setFileContentType(List<String> fileContentType) {
this.fileContentType = fileContentType;
} public String execute() throws Exception
{
for (int i = 0; i < file.size(); i++) {
InputStream is = new FileInputStream(file.get(i));
String root = ServletActionContext.getServletContext().getRealPath("/upload");
File destFile = new File(root,this.getFileFileName().get(i));
OutputStream os = new FileOutputStream(destFile);
//字节数组
byte[] buffer = new byte[400];
int length = 0;
while ((length = is.read(buffer))>0) {
os.write(buffer);
}
is.close();
os.close();
}
return SUCCESS;
}
}

java学习笔记 (8) —— Struts2 实现上传的更多相关文章

  1. struts2学习笔记之十:文件上传

    Struts2的上传 1.Struts2默认采用了apache commons-fileupload 2.Struts2支持三种类型的上传组件 3.需要引入commons-fileupload相关依赖 ...

  2. 学习Struts--Chap07:Struts2文件上传和下载

    1.struts2文件上传 1.1.struts2文件上传的基本概述 在开发web应用的时候,我们一般会为用户提供文件上传的功能,比如用户上传一张图像作为头像等.为了能上传文件,我们必须将表单的met ...

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

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

  4. [原创]java WEB学习笔记50:文件上传案例

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

  5. 笔记:Struts2 文件上传和下载

    为了上传文件必须将表单的method设置为POST,将 enctype 设置为 muiltipart/form-data,只有设置为这种情况下,浏览器才会把用户选择文件的二进制数据发送给服务器. 上传 ...

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

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

  7. php学习笔记:文件的上传(包含设置文件的上传大小限制)

    今天写的是文件上传,前几天自学的正规则又忘记了,用了很笨的方法去判断格式,直接上代码: <?php /** * Created by PhpStorm. * User: Administrato ...

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

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

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

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

随机推荐

  1. thrift的简单实现

    1.使用windows实现,首先在apache官网下载一个thrift的编译工具,在项目中建一个文件叫add.thrift的文件,内容如下: namespace java com.vipshop.sa ...

  2. codevs 3223 素数密度

    题目描述 Description 给定区间[L, R](L <= R <= 2147483647,R-L <= 1000000),请计算区间中素数的个数. 输入描述 Input De ...

  3. Install the Yeoman toolset

    参照:http://yeoman.io/codelab/setup.html 1:$npm install --global yo bower grunt-cli 提示以下错误 npm ERR! /p ...

  4. Qt源码分析之信号和槽机制

    Qt的信号和槽机制是Qt的一大特点,实际上这是和MFC中的消息映射机制相似的东西,要完成的事情也差不多,就是发送一个消息然后让其它窗口响应,当然,这里的消息是广义的说法,简单点说就是如何在一个类的一个 ...

  5. Android双击返回键退出Activity的两种方法

    在开发应用程序的时候,有一种功能是非常常用到的,那就是迅速双击返回按钮,然后实现退出Activity的功能.本人在网上看了很多资料代码,总结起来,主要有两种比较好的方式.一种是开线程延时执行,一种是记 ...

  6. 在ubuntu上编译chrome

    在ubuntu上编译chrome 在ubuntu上编译chrome 红心地瓜 1.获取代码 1)下载tarball,http://chromium-browser-source.commondatas ...

  7. SDL2.0教程翻译·目录

    原文地址:SDL 2.0 Tutorial Index Welcome! 下面的教程旨在为你提供一个SDL2.0以及c++中游戏设计和相关概念的介绍.在本教程中,我们假定你对C++有一定程度上的知识, ...

  8. Response.Expires 属性 (转载于疯狂客的BLOG)

    Expires 属性 Expires 属性指定了在浏览器上缓冲存储的页距过期还有多少时间.如果用户在某个页过期之前又回到此页,就会显示缓冲区中的版本 语法 Response.Expires [= nu ...

  9. HDU_2028——求多个数的最小公倍数

    Problem Description 求n个数的最小公倍数.   Input 输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数.   Output 为每组测试数据输出它们的最 ...

  10. Gradle[1]gradle distZip时,增加目录信息到zip中

    在使用gradle distZip打包生成应用程序时,发现只把src目录,及依赖的本地libs目录下的Jar包达到生成zip中, 如果项目还需要一些库文件,在和src同级目录,如sigar目录下,而且 ...