Servlet中使用getInputStream进行文件上传
据说古老了点,所以代码比较繁琐,主要用于处理文件的地方太多。
下节用SERVLET3.0的Part进行操作一下。
form.html:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html ;charset=UTF-8">
</head>
<body>
<form method="post" action="upload.do" enctype="multipart/form-data">
file: <input type="file" name="filename" value="" /><br>
<input type="submit" value="Upload" name="upload" />
</form>
</body>
</html>
uploadServlet.java:
package cc.openhome;
import java.io.DataInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class UploadServlet
*/
@WebServlet("/upload.do")
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public UploadServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
byte[] body = readBody(request);
String textBody = new String(body, "ISO-8859-1");
String filename = getFilename(textBody);
Position p = getFilePosition(request, textBody);
writeTo(filename, body, p);
}
class Position {
int begin;
int end;
Position(int begin, int end) {
this.begin = begin;
this.end = end;
}
}
private byte[] readBody(HttpServletRequest request)
throws IOException{
int formDataLength = request.getContentLength();
DataInputStream dataStream = new DataInputStream(request.getInputStream());
byte body[] = new byte[formDataLength];
int totalBytes = 0;
while (totalBytes < formDataLength) {
int bytes = dataStream.read(body, totalBytes, formDataLength);
totalBytes += bytes;
}
return body;
}
private Position getFilePosition(HttpServletRequest request, String textBody) throws IOException {
String contentType = request.getContentType();
String boundaryText = contentType.substring(
contentType.lastIndexOf("=") + 1, contentType.length());
int pos = textBody.indexOf("filename=\"");
pos = textBody.indexOf("\n", pos) + 1;
pos = textBody.indexOf("\n", pos) + 1;
pos = textBody.indexOf("\n", pos) + 1;
int boundaryLoc = textBody.indexOf(boundaryText, pos) -4;
int begin = ((textBody.substring(0,
pos)).getBytes("ISO-8859-1")).length;
int end = ((textBody.substring(0,
boundaryLoc)).getBytes("ISO-8859-1")).length;
return new Position(begin, end);
}
private String getFilename(String reqBody) {
String filename = reqBody.substring(
reqBody.indexOf("filename=\"") + 10);
filename = filename.substring(0, filename.indexOf("\n"));
filename = filename.substring(
filename.lastIndexOf("\\") + 1, filename.indexOf("\""));
return filename;
}
private void writeTo(String filename, byte[] body, Position p)
throws FileNotFoundException, IOException {
FileOutputStream fileOutputStream =
new FileOutputStream("c:/workspace/" + filename);
fileOutputStream.write(body, p.begin, (p.end - p.begin));
fileOutputStream.flush();
fileOutputStream.close();
}
}
Servlet中使用getInputStream进行文件上传的更多相关文章
- 分享知识-快乐自己:SpringMvc中的单多文件上传及文件下载
摘要:SpringMvc中的单多文件上传及文件下载:(以下是核心代码(拿过去直接能用)不谢) <!--设置文件上传需要的jar--> <dependency> <grou ...
- zt对于C#中的FileUpload解决文件上传大小限制的问题设置
对于C#中的FileUpload解决文件上传大小限制的问题设置 您可能没意识到,但对于可以使用该技术上载的文件的大小存在限制.默认情况下,使用 FileUpload 控件上载到服务器的文件最大为 4M ...
- servlet 通过 FileItem 实现多文件上传
[本文简介] 一个servlet 多文件上传的简单例子. [依赖包] commons-fileupload-1.3.1.jar commons-io-2.2.jar [依赖包下载] commons-f ...
- springboot中使用分页,文件上传,jquery的具体步骤(持续更新)
分页: pom.xml 加依赖 <dependency> <groupId>com.github.pagehelper</groupId> <arti ...
- Spring中使用StandardServletMultipartResolver进行文件上传
从Spring3.1开始,Spring提供了两个MultipartResolver的实现用于处理multipart请求,分别是:CommonsMultipartResolver和StandardSer ...
- SpringMVC中使用CommonsMultipartResolver进行文件上传
概述: CommonsMultipartResolver是基于Apache的Commons FileUpload来实现文件上传功能的.所以在项目中需要相应的jar文件. FileUpload版本要求1 ...
- 2020最新Servlet+form表单实现文件上传(图片)
servlet实现文件上传接受 这几天学了一点文件上传,有很多不会,在网查了许多博客,但是最新的没有,都比较久了 因为我是小白,版本更新了,以前的方法自己费了好久才弄懂,写个随笔方便以后查找 代码奉上 ...
- java中io流实现文件上传下载
新建io.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" page ...
- JAVA中使用FTPClient实现文件上传下载实例代码
一.上传文件 原理就不介绍了,大家直接看代码吧 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ...
随机推荐
- MySQL 1045登录失败(转)
http://blog.csdn.net/bbirdsky/article/details/8134528# 当你登录MySQL数据库出现:Error 1045错误时(如下图),就表明你输入的用户名或 ...
- JavaScript中什么是包装对象?
存取字符串.数字或布尔值的属性时,创建的临时对象称为包装对象.包装对象只是偶尔用来区分字符串值和字符串对象.数字和数值对象以及布尔值和布尔对象.由于字符串.数字和布尔值的属性都是只读的,并且不能给它们 ...
- C 语言程序员必读的 5 本书,你读过几本?
你正通过看书来学习C语言吗?书籍是知识的丰富来源.你可以从书中学到各种知识.书籍可以毫无歧视地向读者传达作者的本意.C语言是由 Dennis Ritchie在1969年到1973年在贝尔实验室研发的. ...
- StreamingListener技术点
以下是对StreamingListene的研究,由于比较简单,故只贴代码,不做解释 /** * Created by gabry.wu on 2016/5/27. * 实现StreamingListe ...
- 【转】深入理解Java多态原理
之前一直知道多态是什么东西,平时敲代码也经常用到多态,但一直没有真正了解多态底层的运行机制到底是怎么样的,这两天才研究明白点,特地写下来,跟各位同学一起进步,同时也希望各位大神指导和指正. 多态的概念 ...
- win10 激活方法 (各版本)
很多人都在找Win10专业版永久密钥,其实win10激活码不管版本新旧都是通用的,也就是说一个win10专业版key,可以同时激活windows10专业版1809.1803.1709.1703.160 ...
- python中set元素为可迭代元素相加
#a 与 b必须是两个相同类型的可迭代对象 a = "1" b = "2" print(set(a + b)) # {'1', '2'} a = " ...
- Hadoop Hive概念学习系列之hive里的视图(十二)
不多说,直接上干货! 可以先,从MySQL里的视图概念理解入手 视图是由从数据库的基本表中选取出来的数据组成的逻辑窗口,与基本表不同,它是一个虚表.在数据库中,存放的只是视图的定义,而不存放视图包含的 ...
- Xml的读取
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace WebAp ...
- [ SCOI 2005 ] 最大子矩阵
\(\\\) \(Description\) 给出一个\(N\times M\)的有权矩阵,选出其中\(K\)个互不重叠的子矩阵,使得这\(K\)个子矩阵的权值和最大. \(N\in [1,100]\ ...