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中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...
随机推荐
- ICCS 会议 Latex 压缩文件提交主要事项
cd papers/conf latex main... check that the are no error messages ...zip -r mypaper.zip * 说明:必须在Linu ...
- xcopy 提示文件 还是目录
xcopy /y a.jpg .\pics\b.jpg很多次,但xcopy总是问我“文件名还是目录名”gg了一下,发现答案可以这样通过管道来做echo f | xcopy /y a.jpg .\pic ...
- 如何删除 Windows 10 系统生成的 WindowsApps 文件夹
如果曾经修改过 Windows 10 应用安装路径到非系统盘,那么那个盘下就会生成一些文件夹.如果以后重装了系统或者应用删除了,挪位置了,那些文件夹依然在那里——删不掉! 大家都知道这是权限问题,然而 ...
- Objective-C的属性和成员变量用法及关系浅析
在使用Objective-C语言进行了一段时间的iOS开发之后,发现自己的语言基础相对薄弱,于是开始弥补自己的短处.我发现在用过一种语言之后,再回过头来看它的很多原理会发现有更加深刻的理解.下面就对一 ...
- Mac OS X系统 HomeBrew的安装和简单使用
1. 前言 作为linux系统的忠实粉丝,我们都很喜欢 (Debian/Ubuntu)系列的apt包管理系统和(Redhat/Fedora)系列的yum包管理系统. 包括Windows用户都有多种方便 ...
- C#通过StreamWriter对象实现把数值内容写到记事本
本文介绍下,用C#实现将数组内容写到txt文件中的一例代码,有需要的朋友,参考下吧. 代码示例:StreamWriter sw=new StreamWriter("F:\\ex.txt&qu ...
- 笔记:加密 RSA AES
笔记:加密 RSA AES RSA 是非对称加密,有公钥和私钥. RSA算法原理(一) http://www.ruanyifeng.com/blog/2013/06/rsa_algorithm_par ...
- The SDK platform-tools version ((21)) is too old to check APIs compiled with API 23
android studio是个坑爹的工具,每次打开文件头都出现如上错误提示. 解决方法: Update your android sdk platform-tools to the revision ...
- 框架(yii和thinkphp)中实例化php内置或者扩展中的对象问题
将php原生语句实例化SphinxClient对象移植到yii2框架中报错 原生语句中这样写: $s = new SphinxClient(); 框架中应该加入反斜杠,这样写: $s = new \S ...
- bzoj4598: [Sdoi2016]模式字符串
Description 给出n个结点的树结构T,其中每一个结点上有一个字符,这里我们所说的字符只考虑大写字母A到Z,再给出长度为m 的模式串s,其中每一位仍然是A到z的大写字母.Alice希望知道,有 ...