Servlet实现文件上传(深度)(二)
struts.action.extension=action <!--以.action为我们提交的后缀名-->
struts.multipart.saveDir=c:/shengsiyuan <!--保存我们上传文件的临时目录,此文件夹下面会生成临时文件,以.tmp为后缀名-->
struts.multipart.maxSize=1048576000 <!--规定文件的大小-->
当然以上文件的大小界定我们还可以这样在struts.xml文件中定义如下
<struts>
<constant name="struts.multipart.maxSize" value="1048576000"></constant>
</struts>
2、页面fileUpload.jsp用于上传文件
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>
<base href="<%=basePath%>">
<title>My JSP 'fileUpload.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="fileUpload.action" method="post" enctype="multipart/form-data">
username: <input type="text" name="username"><br>
file: <input type="file" name="file"><br>
<input type="submit" value="submit">
</form>
</body>
</html>
3、UploadAction.java 用于处理上传的文件的action
package com.shengsiyuan.struts2;
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 String username; //传入的username
private File file; //以File为类型的file
private String fileFileName; //小心命名,以file开头FileName固定
private String fileContentType;
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
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;
}
@Override
public String execute() throws Exception
{
String root = ServletActionContext.getRequest().getRealPath("/upload");//定义上传文件的路径
InputStream is = new FileInputStream(file);
//这里查看上传文件临时路径、以及临时文件的名字,在struts.properties已规定
System.out.println("path: " + file.getAbsolutePath());
System.out.println("file: " + file.getName());
//这里查看文件的真实名称
System.out.println("fileFileName: " + fileFileName);
File destFile = new File(root, fileFileName);
OutputStream os = new FileOutputStream(destFile);
byte[] buffer = new byte[400];//定义以400字节的速度上传文件
int length = 0;
while(-1 != (length = is.read(buffer)))
{
os.write(buffer, 0, length);
// Thread.sleep(1000);//为了能够查看临时文件存在,定义线程延时1秒
}
is.close();
os.close();
return SUCCESS;
}
}
4、结果展示页fileUploadResult.jsp
<%@ 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>
<base href="<%=basePath%>">
<title>My JSP 'fileUploadResult.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
username: <s:property value="username"/><br>
name:<s:property value="fileFileName"/>
<br>
type:<s:property value="fileContentType"/>
</body>
</html>
5、忘记struts.xml文件的配置了,你自己添加把
Servlet实现文件上传(深度)(二)的更多相关文章
- Servlet实现文件上传
一.Servlet实现文件上传,需要添加第三方提供的jar包 下载地址: 1) commons-fileupload-1.2.2-bin.zip : 点击打开链接 2) commons- ...
- Servlet实现文件上传,可多文件上传
一.Servlet实现文件上传,需要添加第三方提供的jar包 接着把这两个jar包放到 lib文件夹下: 二: 文件上传的表单提交方式必须是POST方式, 编码类型:enctype="mul ...
- 配置servlet支持文件上传
Servlet3.0为Servlet添加了multipart配置选项,并为HttpServletRequest添加了getPart和getParts方法获取上传文件.为了使Servlet支付文件上传需 ...
- jsp+servlet实现文件上传下载
相关素材下载 01.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" ...
- java commons-fileupload servlet 多文件上传
commons-fileupload servlet 多文件上传 需要引入的 jar 包. commons-fileupload-1.3.2.jar commons-io-2.2.jar 工程路劲:查 ...
- servlet web文件上传
web文件上传也是一种POST方式,特别之处在于,需设置FORM的enctype属性为multipart/form-data. 并且需要使用文件域. servlet的代码比较关键是这几句: // 使用 ...
- servlet实现文件上传,预览,下载和删除
一.准备工作 1.1 文件上传插件:uploadify: 1.2 文件上传所需jar包:commons-fileupload-1.3.1.jar和commons-io-2.2.jar 1.3 将数 ...
- Servlet实现文件上传和下载
对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用Servlet获取上传文件的输入流然后再解析里面的请求参数是比较麻烦,所以一般选择采用apache的开源工具commo ...
- jsp+servlet实现文件上传
上传(上传不能使用BaseServlet) 1. 上传对表单限制 * method="post" * enctype="multipart/form-data" ...
随机推荐
- docker的一些常用命令整理
docker清除命令: 杀掉所有容器:docker kill $(docker ps -q) ; 清除所有容器:docker rm $(docker ps -a -q) ; 清除所有镜像:docker ...
- 最近关于ACM训练与算法的总结
到了大四以后越来越意识到基础知识的重要性,很多高屋建瓴的观点与想法都是建立在坚实的基础之上的, 招式只有在强劲的内力下才能发挥最大的作用,曾经有段时间我有这样的想法:我们出去以后和其他 ...
- java 文件字节输出流
Example10_5.java import java.io.*; public class Example10_5 { public static void main(String args[]) ...
- CodeForces 696A Lorenzo Von Matterhorn (LCA + map)
方法:求出最近公共祖先,使用map给他们计数,注意深度的求法. 代码如下: #include<iostream> #include<cstdio> #include<ma ...
- android 报错之noclassdeffounderror
解决方案1: 导入第3方jar包问题,明明导入了jar但还是报java.lang.NoClassDefFoundError解决步骤:1.在Android项目根目录下新建一个lib文件夹:2.把你需要导 ...
- 转:Jmeter--google plugin插件监控被测系统资源方法
一.插件准备 1.插件下载地址 http://jmeter-plugins.org/downloads/all/ 以下有两个版本的,1.1.2和1.1.3,注意Jmeter版本 1.1.2支持Jmet ...
- 一个设置 material design icon的插件工具
一个设置 material design icon的插件工具 github地址:https://github.com/konifar/android-material-design-icon-gene ...
- opencv mat 转灰度图
Imgproc.cvtColor(sshotmat, sshotmatgray, Imgproc.COLOR_BGR2GRAY); 更多参数看 public class Imgproc { priv ...
- HTTP状态码 - HTTP Status Code
HTTP Status Code 常见的状态码: HTTP: Status 200 – 服务器成功返回网页HTTP: Status 404 – 请求的网页不存在HTTP: Status 503 – 服 ...
- 转 Linux进程状态分析
众所周知,现在的分时操作系统能够在一个CPU上运行多个程序,让这些程序表面上看起来是在同时运行的.linux就是这样的一个操作系统.在linux系统中,每个被运行的程序实例对应一个或多个进程.l ...