java学习笔记 (8) —— Struts2 实现上传
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 实现上传的更多相关文章
- struts2学习笔记之十:文件上传
Struts2的上传 1.Struts2默认采用了apache commons-fileupload 2.Struts2支持三种类型的上传组件 3.需要引入commons-fileupload相关依赖 ...
- 学习Struts--Chap07:Struts2文件上传和下载
1.struts2文件上传 1.1.struts2文件上传的基本概述 在开发web应用的时候,我们一般会为用户提供文件上传的功能,比如用户上传一张图像作为头像等.为了能上传文件,我们必须将表单的met ...
- [原创]java WEB学习笔记49:文件上传基础,基于表单的文件上传,使用fileuoload 组件
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- [原创]java WEB学习笔记50:文件上传案例
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- 笔记:Struts2 文件上传和下载
为了上传文件必须将表单的method设置为POST,将 enctype 设置为 muiltipart/form-data,只有设置为这种情况下,浏览器才会把用户选择文件的二进制数据发送给服务器. 上传 ...
- PHP学习笔记--文件目录操作(文件上传实例)
文件操作是每个语言必须有的,不仅仅局限于PHP,这里我们就仅用PHP进行讲解 php的文件高级操作和文件上传实例我放在文章的最后部分.--以后我还会给大家写一个PHP类似于网盘操作的例子 注意:阅读此 ...
- php学习笔记:文件的上传(包含设置文件的上传大小限制)
今天写的是文件上传,前几天自学的正规则又忘记了,用了很笨的方法去判断格式,直接上代码: <?php /** * Created by PhpStorm. * User: Administrato ...
- PHP学习笔记 02 之文件上传
我们了解了表单传值后,这些我就可以完成PHP的文件上传了.我们了解PHP文件上传前,先了解PHP文件上传的原理. 一.PHP上传文件原理 第一步:将本地的文件通过form表单上传到服务器的临时目录中, ...
- Kali学习笔记38:文件上传漏洞
早些年,提到Web渗透,或者搜索一些黑客教程 基本都会看到文件上传漏洞. 它是一个很经典的漏洞 但它本质其实不是一个漏洞,而是网站本身的上传文件功能 不过如果我们上传了Webshell,那么就成为了文 ...
随机推荐
- python 装饰器简介
24.装饰器 1.@ + 函数名:#1自动执行outer函数并且将下面的函数名f1当做参数传递 #2将outer函数的返回值,重新赋值给f1. def ou ...
- 编写css代码的方式
css(层叠样式表) 在一个网页中主要负责了页面的数据样式. 编写css代码的方式: 第一种: 在style标签中编写css代码. 只能用于本页面中,复用性不强 ...
- java之多线程的理解
线程的属性 (1)线程的状态 线程在它的生命周期中可能处于以下几种状态之一: New(新生):线程对象刚刚被创建出来: Runnable(可运行):在线程对象上调用start方法后,相应线程便 ...
- Android中开发Service
Service的开发分为两个步骤:定义Service和配置Service1.定义Service定义一个Service子类继承于Service2.配置Service在AndroidManifest.xm ...
- Linux之V4L2视频采集编程详解
V4L2(Video For Linux Two) 是内核提供给应用程序访问音.视频驱动的统一接口. Linux系统中,视频设备被当作一个设备文件来看待,设备文件存放在 /dev目录下,完整路径的设 ...
- NOI2014 D2T3 购票 简单粗暴解法(凸包维护)
之前说过这题能用点分治(详见 http://www.cnblogs.com/jasonyu/p/noi2014.html),但其实还有更粗暴的解法. 要求出一个点的答案,我们需要知道树上一段路径的点形 ...
- Gson 基础教程 —— 自定义类型适配器(TypeAdapter)
1,实现一个类型适配器(TypeAdapter) 自定义类型适配器需要实现两个接口: JsonSerializer<T> JsonDeserializer<T> 和两个方法: ...
- [转载]SQL Server查找包含某关键字的存储过程3种方法
存储过程都写在一个指定的表中了,我们只要使用like查询就可以实现查询当前这台SQL Server中所有存储过程中包括了指定关键字的存储过程并显示出来,下面一起来看看我总结了几条命令. 例子1 代码如 ...
- (转)iOS keychain API及其封装
一. Keychain API KeyChain中item的结构为: 1.增加keychain Item OSStatus SecItemAdd (CFDictionaryRef attributes ...
- 【剑指offer】二叉搜索树的后序遍历序列
转载请注明出处:http://blog.csdn.net/ns_code/article/details/26092725 剑指offer上的第24题,主要考察递归思想,九度OJ上AC. 题目描写叙述 ...