Rhythmk 一步一步学 JAVA (17):Servlet 文件上传
1、环境 : JDK 1.6 , Tomcat 7.0
2、第三方类库:
commons-fileupload-1.3.1.jar
commons-io-2.4.jar
3、web.xml配置:
<servlet>
<servlet-name>FileUpload</servlet-name>
<servlet-class>java02.rhythmk.com.FileUploadServlet</servlet-class> <init-param>
<param-name>filepath</param-name>
<param-value>uploadfile</param-value>
</init-param>
<init-param>
<param-name>temppath</param-name>
<param-value>temp</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>FileUpload</servlet-name>
<url-pattern>/FileUpload.html</url-pattern>
</servlet-mapping>
4、 jsp :
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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=utf-8">
<title>Insert title here</title>
</head>
<body>
<!-- enctype 默认是 application/x-www-form-urlencoded -->
<form action="FileUpload.html" enctype="multipart/form-data" method="post" > rhythmk::<input type="text" name="rhythmk"> <br/>
上传文件:<input type="file" name="file1"><br/> <input type="submit" value="提交"/> </form> </body>
</html>
5、Servlet:
package java02.rhythmk.com; import java.io.File;
import java.io.IOException;
import java.util.List; import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload; /**
* Servlet implementation class FileUploadServlet
*/
@WebServlet("/FileUploadServlet")
public class FileUploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L; private static String filepath = "";
private static String temp = ""; /**
* Default constructor.
*/
public FileUploadServlet() {
// TODO Auto-generated constructor stub } @Override
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
super.init(config); if (filepath == "" || temp == "") {
filepath = config.getInitParameter("filepath");
temp = config.getInitParameter("temppath"); ServletContext context = getServletContext();
filepath = context.getRealPath(filepath);
temp = context.getRealPath(temp); System.out.println("filepath:" + filepath + ",temp:" + temp);
}
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/ protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8"); try { DiskFileItemFactory foctory = new DiskFileItemFactory(); // 设置临时文件
foctory.setRepository(new File(temp));
// 设置临时文件缓存区大小
foctory.setSizeThreshold(1024 * 1024); ServletFileUpload sFileUpload = new ServletFileUpload(foctory); List<FileItem> list = (List<FileItem>) sFileUpload
.parseRequest(request); for (FileItem item : list) {
String name = item.getFieldName();
if (item.isFormField()) {
System.out.println("filedName:" + name + ",value:"
+ item.getString()); } else { String filename = item.getName(); System.out.println("value:" + filename); if (filename.length() > 0) {
System.out.println("文件保存路径:" + filepath + "\\"
+ filename); File file = new File(filepath);
if (!file.exists()) {
file.mkdir();
} // 保存文件
item.write(new File(filepath, filename)); response.getWriter().write(
"文件保存路径:" + filepath + "\\" + filename);
} } } } catch (Exception e) {
e.printStackTrace();
} } }
Rhythmk 一步一步学 JAVA (17):Servlet 文件上传的更多相关文章
- Java中实现文件上传下载的三种解决方案
第一点:Java代码实现文件上传 FormFile file=manform.getFile(); String newfileName = null; String newpathname=null ...
- Java FtpClient 实现文件上传服务
一.Ubuntu 安装 Vsftpd 服务 1.安装 sudo apt-get install vsftpd 2.添加用户(uftp) sudo useradd -d /home/uftp -s /b ...
- 【原创】用JAVA实现大文件上传及显示进度信息
用JAVA实现大文件上传及显示进度信息 ---解析HTTP MultiPart协议 (本文提供全部源码下载,请访问 https://github.com/grayprince/UploadBigFil ...
- Java下载https文件上传到阿里云oss服务器
Java下载https文件上传到阿里云oss服务器 今天做了一个从Https链接中下载音频并且上传到OSS服务器,记录一下希望大家也少走弯路. 一共两个类: 1 .实现自己的证书信任管理器类 /** ...
- 【Java】JavaWeb文件上传和下载
文件上传和下载在web应用中非常普遍,要在jsp环境中实现文件上传功能是非常容易的,因为网上有许多用java开发的文件上传组件,本文以commons-fileupload组件为例,为jsp应用添加文件 ...
- java中的文件上传下载
java中文件上传下载原理 学习内容 文件上传下载原理 底层代码实现文件上传下载 SmartUpload组件 Struts2实现文件上传下载 富文本编辑器文件上传下载 扩展及延伸 学习本门课程需要掌握 ...
- java+web+大文件上传下载
文件上传是最古老的互联网操作之一,20多年来几乎没有怎么变化,还是操作麻烦.缺乏交互.用户体验差. 一.前端代码 英国程序员Remy Sharp总结了这些新的接口 ,本文在他的基础之上,讨论在前端采用 ...
- Java开发系列-文件上传
概述 Java开发中文件上传的方式有很多,常见的有servlet3.0.common-fileUpload.框架.不管哪种方式,对于文件上传的本质是不变的. 文件上传的准备 文件上传需要客户端跟服务都 ...
- java web学习总结(二十四) -------------------Servlet文件上传和下载的实现
在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...
随机推荐
- 掌握Git撤销操作,随心所欲控制文件状态
本文主要讨论和撤销有关的 git 操作.目的是让读者在遇到关于撤销问题时能够方便迅速对照执行解决问题,而不用去翻阅参数繁多的 git 使用说明. 一开始你只需了解大致功能即可,不必记住所有命令和具体参 ...
- my_itoa
#include <iostream> using namespace std; char *my_reverse(char* s) { char *p,*q; p=s;q=s; whil ...
- [持续更新]Python 笔记
本文以 Python 2.7 为基础. lambda 函数实现递归 方法一:传递一个 self 参数 求阶乘: frac = lambda self, x: self(self, x - 1) * x ...
- Java调用函数传递参数到底是值传递还是引用传递
今天翻看微信上有关Java技术的公众号时,看到了一篇关于Java中值传递的问题,文章讨论了在Java中调用函数进行传参的时候到底是值传递还是引用传递这个面试时会问到的问题.之前也接触过类似的问题,但只 ...
- Oracle中用exp/imp命令快速导入导出数据
from: http://blog.csdn.net/wangchunyu11155/article/details/53635602 [用 exp 数 据 导 出]: 1 将数据库TEST完全导出, ...
- oracle-分区(笔记)
partition by 用于指定分区方式 range 表示分区的方式是范围划分 partition pn 用于指定分区的名字 values less than 指定分区的上界(上限) ------- ...
- CentOS解压rar文件
默认不能解压rar文件. 进官网下载:http://www.rarsoft.com/download.htm RAR 5.40 for Linux x64 安装: # tar -zxvf rarlin ...
- CentOS常用基础命令汇总
1.关机 (系统的关机.重启以及登出 ) 的命令 shutdown -h now 关闭系统(1) init 0 关闭系统(2) telinit 0 关闭系统(3) shutdown -h hours: ...
- 关于matlab中定点数overflow的处理办法
定点数overflow的处理有两种办法:1,saturate,也就是说如果超过定点的最大值就取最大值,例如最大值是6结果是8,那么就取6:2,wrap,就是循环,如下图所示
- BW数据加载
BW数据加载的优先级 1.主数据属性的加载 步骤图 从下到上 1)运行InfoPackage加载到PSA 找到主数据属性的InfoPackage,双击 点击Start按钮 点击监视器,查看运 ...