- 浏览: 10915 次
- 性别:
- 来自: 广州
最新评论
package com.upload.http;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletInputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
public class MultipartRequest implements HttpServletRequest{
private static final String TEMP_PATH = "c:/temp/";
private long userId_;
private HttpServletRequest req_;
private File dir_ = new File(TEMP_PATH);
private HashMap<String, ArrayList<String>> parameters_ = new HashMap<String, ArrayList<String>>();
private HashMap<String, UploadedFile> files_ = new HashMap<String, UploadedFile>();
private HashMap<File, String> exactFileNameTable_ = new HashMap<File, String>();
public static boolean isMultipart(HttpServletRequest req) {
String type = null;
String type1 = req.getHeader("Content-Type");
String type2 = req.getContentType();
if (type1 == null && type2 != null) {
type = type2;
}
else if (type2 == null && type1 != null) {
type = type1;
}
else if (type1 != null && type2 != null) {
type = (type1.length() > type2.length() ? type1 : type2);
}
if (type == null ||!type.toLowerCase().startsWith("multipart/form-data")) {
return false ;
}else{
return true ;
}
}
public MultipartRequest(HttpServletRequest req, long userId) throws IOException {
if (req == null)
throw new IllegalArgumentException("request cannot be null");
if (!dir_.isDirectory())
dir_.mkdir();
if (!dir_.canWrite())
throw new IllegalArgumentException("服务器上 " + TEMP_PATH + " 这个目录没有写的权限");
req_ = req;
userId_ = userId;
readRequest();
}
protected void readRequest()
throws IOException {
int length = req_.getContentLength();
String type = null;
String type1 = req_.getHeader("Content-Type");
String type2 = req_.getContentType();
if (type1 == null && type2 != null) {
type = type2;
}
else if (type2 == null && type1 != null) {
type = type1;
}
else if (type1 != null && type2 != null) {
type = (type1.length() > type2.length() ? type1 : type2);
}
if (type == null ||
!type.toLowerCase().startsWith("multipart/form-data")) {
throw new IOException("Posted content type isn't multipart/form-data");
}
String boundary = extractBoundary(type);
if (boundary == null) {
throw new IOException("Separation boundary was not specified");
}
MultipartInputStreamHandler in =
new MultipartInputStreamHandler(req_.getInputStream(), length);
String line = in.readLine();
if (line == null) {
throw new IOException("Corrupt form data: premature ending");
}
if (!line.startsWith(boundary)) {
throw new IOException("Corrupt form data: no leading boundary");
}
boolean done = false;
while (!done) {
done = readNextPart(in, boundary);
}
}
private String extractBoundary(String line) {
int index = line.lastIndexOf("boundary=");
if (index == -1) {
return null;
}
String boundary = line.substring(index + 9);
boundary = "--" + boundary;
return boundary;
}
protected boolean readNextPart(MultipartInputStreamHandler in,
String boundary)
throws IOException {
String line = in.readLine();
if (line == null) {
return true;
}
else if (line.length() == 0) {
return true;
}
String[] dispInfo = extractDispositionInfo(line);
String strHttpParameterName = dispInfo[1];
String strExactFileName = dispInfo[2];
line = in.readLine();
if (line == null) {
return true;
}
String contentType = extractContentType(line);
if (contentType != null) {
line = in.readLine();
if (line == null || line.length() > 0) {
throw new IOException("Malformed line after content type: " + line);
}
}
else {
contentType = "application/octet-stream";
}
if (strExactFileName == null) {
String value = readParameter(in, boundary);
ArrayList<String> existingValues = (ArrayList<String>)parameters_.get(strHttpParameterName);
if (existingValues == null) {
existingValues = new ArrayList<String>();
parameters_.put(strHttpParameterName, existingValues);
}
existingValues.add(value);
}
else {
File temporaryFile;
if (!strExactFileName.equals("unknown")) {
String tempfilename = userId_ + strHttpParameterName
+ System.currentTimeMillis() + ".bin";
temporaryFile = new File(dir_, tempfilename);
} else {
temporaryFile = null;
}
readAndSaveFile(in, boundary, temporaryFile, contentType);
if (!strExactFileName.equals("unknown")) {
files_.put(strHttpParameterName,
new UploadedFile(temporaryFile, contentType));
}
exactFileNameTable_.put(temporaryFile, strExactFileName);
}
return false;
}
protected void readAndSaveFile(MultipartInputStreamHandler in,
String boundary,
File temporaryFile,
String contentType) throws IOException {
OutputStream out = null;
if (temporaryFile == null) {
out = new ByteArrayOutputStream();
} else {
if (contentType.equals("application/x-macbinary")){
out = new MacBinaryDecoderOutputStream(
new BufferedOutputStream(
new FileOutputStream(temporaryFile), 8 * 1024));
}
else {
out = new BufferedOutputStream(
new FileOutputStream(temporaryFile), 8 * 1024);
}
}
byte[] bbuf = new byte[100 * 1024];
int result;
String line;
boolean rnflag = false;
while ((result = in.readLine(bbuf, 0, bbuf.length)) != -1) {
if (result > 2 && bbuf[0] == '-' && bbuf[1] == '-') {
line = new String(bbuf, 0, result, "GB2312");
if (line.startsWith(boundary)) break;
}
if (rnflag) {
out.write('\r'); out.write('\n');
rnflag = false;
}
if (result >= 2 &&
bbuf[result - 2] == '\r' &&
bbuf[result - 1] == '\n') {
out.write(bbuf, 0, result - 2);
rnflag = true;
}
else {
out.write(bbuf, 0, result);
}
}
out.close();
}
private String[] extractDispositionInfo(String line) throws IOException {
String[] retval = new String[3];
String origline = line;
line = origline.toLowerCase();
int start = line.indexOf("content-disposition: ");
int end = line.indexOf(";");
if (start == -1 || end == -1) {
throw new IOException("Content disposition corrupt: " + origline);
}
String disposition = line.substring(start + 21, end);
if (!disposition.equals("form-data")) {
throw new IOException("Invalid content disposition: " + disposition);
}
start = line.indexOf("name=\"", end);
end = line.indexOf("\"", start + 7);
if (start == -1 || end == -1) {
throw new IOException("Content disposition corrupt: " + origline);
}
String name = origline.substring(start + 6, end);
String filename = null;
start = line.indexOf("filename=\"", end + 2);
end = line.indexOf("\"", start + 10);
if (start != -1 && end != -1) {
filename = origline.substring(start + 10, end);
int slash =
Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'));
if (slash > -1) {
filename = filename.substring(slash + 1);
}
if (filename.equals("")) filename = "unknown";
}
retval[0] = disposition;
retval[1] = name;
retval[2] = filename;
return retval;
}
private String extractContentType(String line) throws IOException {
String contentType = null;
String origline = line;
line = origline.toLowerCase();
if (line.startsWith("content-type")) {
int start = line.indexOf(" ");
if (start == -1) {
throw new IOException("Content type corrupt: " + origline);
}
contentType = line.substring(start + 1);
}
else if (line.length() != 0) {
throw new IOException("Malformed line after disposition: " + origline);
}
return contentType;
}
protected String readParameter(MultipartInputStreamHandler in,
String boundary) throws IOException {
StringBuffer sbuf = new StringBuffer();
String line;
while ((line = in.readLine()) != null) {
if (line.startsWith(boundary)) break;
sbuf.append(line + "\r\n");
}
if (sbuf.length() == 0) {
return "";
}
sbuf.setLength(sbuf.length() - 2);
return sbuf.toString();
}
public File getFile(String strParameterName) {
UploadedFile file = (UploadedFile)files_.get(strParameterName);
if (file != null) {
return file.getFile();
} else {
return null;
}
}
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletInputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
public class MultipartRequest implements HttpServletRequest{
private static final String TEMP_PATH = "c:/temp/";
private long userId_;
private HttpServletRequest req_;
private File dir_ = new File(TEMP_PATH);
private HashMap<String, ArrayList<String>> parameters_ = new HashMap<String, ArrayList<String>>();
private HashMap<String, UploadedFile> files_ = new HashMap<String, UploadedFile>();
private HashMap<File, String> exactFileNameTable_ = new HashMap<File, String>();
public static boolean isMultipart(HttpServletRequest req) {
String type = null;
String type1 = req.getHeader("Content-Type");
String type2 = req.getContentType();
if (type1 == null && type2 != null) {
type = type2;
}
else if (type2 == null && type1 != null) {
type = type1;
}
else if (type1 != null && type2 != null) {
type = (type1.length() > type2.length() ? type1 : type2);
}
if (type == null ||!type.toLowerCase().startsWith("multipart/form-data")) {
return false ;
}else{
return true ;
}
}
public MultipartRequest(HttpServletRequest req, long userId) throws IOException {
if (req == null)
throw new IllegalArgumentException("request cannot be null");
if (!dir_.isDirectory())
dir_.mkdir();
if (!dir_.canWrite())
throw new IllegalArgumentException("服务器上 " + TEMP_PATH + " 这个目录没有写的权限");
req_ = req;
userId_ = userId;
readRequest();
}
protected void readRequest()
throws IOException {
int length = req_.getContentLength();
String type = null;
String type1 = req_.getHeader("Content-Type");
String type2 = req_.getContentType();
if (type1 == null && type2 != null) {
type = type2;
}
else if (type2 == null && type1 != null) {
type = type1;
}
else if (type1 != null && type2 != null) {
type = (type1.length() > type2.length() ? type1 : type2);
}
if (type == null ||
!type.toLowerCase().startsWith("multipart/form-data")) {
throw new IOException("Posted content type isn't multipart/form-data");
}
String boundary = extractBoundary(type);
if (boundary == null) {
throw new IOException("Separation boundary was not specified");
}
MultipartInputStreamHandler in =
new MultipartInputStreamHandler(req_.getInputStream(), length);
String line = in.readLine();
if (line == null) {
throw new IOException("Corrupt form data: premature ending");
}
if (!line.startsWith(boundary)) {
throw new IOException("Corrupt form data: no leading boundary");
}
boolean done = false;
while (!done) {
done = readNextPart(in, boundary);
}
}
private String extractBoundary(String line) {
int index = line.lastIndexOf("boundary=");
if (index == -1) {
return null;
}
String boundary = line.substring(index + 9);
boundary = "--" + boundary;
return boundary;
}
protected boolean readNextPart(MultipartInputStreamHandler in,
String boundary)
throws IOException {
String line = in.readLine();
if (line == null) {
return true;
}
else if (line.length() == 0) {
return true;
}
String[] dispInfo = extractDispositionInfo(line);
String strHttpParameterName = dispInfo[1];
String strExactFileName = dispInfo[2];
line = in.readLine();
if (line == null) {
return true;
}
String contentType = extractContentType(line);
if (contentType != null) {
line = in.readLine();
if (line == null || line.length() > 0) {
throw new IOException("Malformed line after content type: " + line);
}
}
else {
contentType = "application/octet-stream";
}
if (strExactFileName == null) {
String value = readParameter(in, boundary);
ArrayList<String> existingValues = (ArrayList<String>)parameters_.get(strHttpParameterName);
if (existingValues == null) {
existingValues = new ArrayList<String>();
parameters_.put(strHttpParameterName, existingValues);
}
existingValues.add(value);
}
else {
File temporaryFile;
if (!strExactFileName.equals("unknown")) {
String tempfilename = userId_ + strHttpParameterName
+ System.currentTimeMillis() + ".bin";
temporaryFile = new File(dir_, tempfilename);
} else {
temporaryFile = null;
}
readAndSaveFile(in, boundary, temporaryFile, contentType);
if (!strExactFileName.equals("unknown")) {
files_.put(strHttpParameterName,
new UploadedFile(temporaryFile, contentType));
}
exactFileNameTable_.put(temporaryFile, strExactFileName);
}
return false;
}
protected void readAndSaveFile(MultipartInputStreamHandler in,
String boundary,
File temporaryFile,
String contentType) throws IOException {
OutputStream out = null;
if (temporaryFile == null) {
out = new ByteArrayOutputStream();
} else {
if (contentType.equals("application/x-macbinary")){
out = new MacBinaryDecoderOutputStream(
new BufferedOutputStream(
new FileOutputStream(temporaryFile), 8 * 1024));
}
else {
out = new BufferedOutputStream(
new FileOutputStream(temporaryFile), 8 * 1024);
}
}
byte[] bbuf = new byte[100 * 1024];
int result;
String line;
boolean rnflag = false;
while ((result = in.readLine(bbuf, 0, bbuf.length)) != -1) {
if (result > 2 && bbuf[0] == '-' && bbuf[1] == '-') {
line = new String(bbuf, 0, result, "GB2312");
if (line.startsWith(boundary)) break;
}
if (rnflag) {
out.write('\r'); out.write('\n');
rnflag = false;
}
if (result >= 2 &&
bbuf[result - 2] == '\r' &&
bbuf[result - 1] == '\n') {
out.write(bbuf, 0, result - 2);
rnflag = true;
}
else {
out.write(bbuf, 0, result);
}
}
out.close();
}
private String[] extractDispositionInfo(String line) throws IOException {
String[] retval = new String[3];
String origline = line;
line = origline.toLowerCase();
int start = line.indexOf("content-disposition: ");
int end = line.indexOf(";");
if (start == -1 || end == -1) {
throw new IOException("Content disposition corrupt: " + origline);
}
String disposition = line.substring(start + 21, end);
if (!disposition.equals("form-data")) {
throw new IOException("Invalid content disposition: " + disposition);
}
start = line.indexOf("name=\"", end);
end = line.indexOf("\"", start + 7);
if (start == -1 || end == -1) {
throw new IOException("Content disposition corrupt: " + origline);
}
String name = origline.substring(start + 6, end);
String filename = null;
start = line.indexOf("filename=\"", end + 2);
end = line.indexOf("\"", start + 10);
if (start != -1 && end != -1) {
filename = origline.substring(start + 10, end);
int slash =
Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'));
if (slash > -1) {
filename = filename.substring(slash + 1);
}
if (filename.equals("")) filename = "unknown";
}
retval[0] = disposition;
retval[1] = name;
retval[2] = filename;
return retval;
}
private String extractContentType(String line) throws IOException {
String contentType = null;
String origline = line;
line = origline.toLowerCase();
if (line.startsWith("content-type")) {
int start = line.indexOf(" ");
if (start == -1) {
throw new IOException("Content type corrupt: " + origline);
}
contentType = line.substring(start + 1);
}
else if (line.length() != 0) {
throw new IOException("Malformed line after disposition: " + origline);
}
return contentType;
}
protected String readParameter(MultipartInputStreamHandler in,
String boundary) throws IOException {
StringBuffer sbuf = new StringBuffer();
String line;
while ((line = in.readLine()) != null) {
if (line.startsWith(boundary)) break;
sbuf.append(line + "\r\n");
}
if (sbuf.length() == 0) {
return "";
}
sbuf.setLength(sbuf.length() - 2);
return sbuf.toString();
}
public File getFile(String strParameterName) {
UploadedFile file = (UploadedFile)files_.get(strParameterName);
if (file != null) {
return file.getFile();
} else {
return null;
}
}
发表评论
-
求一份fm收音机源码
2011-09-22 14:51 1228本人想做一个FM收音机,但是毫无思路,求高人给一份fm收音机源 ... -
出现一个异常无法解决
2011-04-21 11:19 840com.mysql.jdbc.exceptions.jdbc4 ... -
关于hibernate取值问题,跪求高手帮帮忙
2010-10-19 15:33 1190异常:org.hibernate.exceptio ... -
http上传下载(3)
2010-05-02 23:39 802接类MultipartRequest.java文件尾部 pu ... -
http上传下载(1)
2010-05-02 23:35 941PubDun package com.upload.htt ... -
关于java的http协议上传
2010-05-02 23:07 755http://blog.sina.com.cn/s/blog_ ... -
ftp上传和下载
2010-05-01 13:05 776Java代码 1.package com.northking ... -
采用HTTP协议上传文件实现(java)(转载)
2010-04-29 23:25 1174通过ServletRequest类的getInputStrea ... -
用Java 开发 WebService Axis简单实例
2010-04-29 22:48 785在开发之前, 先了解一下AXIS,axis目前出现的2版本以其 ... -
webservice之axis介绍
2010-04-29 22:45 807webservice之axis介绍1 Axis全称Apache ... -
ftp上传文件
2010-04-29 22:32 776在一般的web应用中,文件上传可以分为两类(ftp上传和htt ...
相关推荐
### HTTP上传下载C++代码实现解析 #### 一、概览 本文将深入解析一个用于HTTP上传下载功能的C++代码实现案例。该案例通过一个名为`CZZHttp`的类来封装HTTP上传与下载的核心逻辑。代码示例中不仅包括了基本的文件...
本篇文章将深入探讨使用C++封装的CHttpClient类,它专门用于实现HTTP文件的上传和下载功能。 首先,让我们了解HTTP(超文本传输协议)。HTTP是一种应用层协议,用于在Web上交换各种类型的数据。它基于请求-响应模型...
自己封装了一下异步上传下载方便调用 文件下载做了断点续传处理 由于我这边资源版本是在后面加上数字区别 所以 保存的文件名大概是这样 F:/815319d16_1.scene 如果有新版本将会保存成这样 F:/815319d16_2.scene...
在这个场景下,我们将关注如何使用HTTP协议来实现文件的上传和下载。 文件上传通常涉及到客户端(如网页表单、应用程序)向服务器发送数据。在HTTP中,文件上传可以通过POST请求实现。POST请求携带的数据可以在请求...
c++ 实现,在 linux 平台测试通过,windows 上原理是一样的,下载上传大文件没有问题,
在C# WinForm应用中,实现HTTP文件上传和下载功能是一项常见的需求,特别是在构建桌面应用程序时。本主题将深入探讨如何使用HTTP协议进行文件的上传和下载,并且结合进度条来展示操作进度,同时实时显示下载和上传的...
在IT行业中,HTTP上传下载是网络应用的基本功能之一,它涉及到客户端与服务器之间的数据传输。这里我们有一个名为“http上传下载功能的一个段小代码程序”的项目,它提供了一个简单的实现来处理HTTP上的资源上传和...
在IT行业中,HTTPUtils是一个常见的工具类,通常用于简化HTTP请求的处理,特别是涉及文件上传和下载的操作。在这个源代码库中,我们看到一个名为"HTTPUtils.java"的文件,它很可能封装了处理HTTP请求的核心功能。...
HTTP上传和下载是其核心功能之一,尤其在网页交互、文件共享等方面扮演着重要角色。 **HTTP上传** HTTP上传是指通过HTTP协议将本地数据(如图片、文档等)发送到远程服务器的过程。这一过程通常涉及到以下几个关键...
在Struts2中,文件上传和下载功能是常见的需求,它允许用户在Web应用中交换文件,例如提交图片、文档或者下载资源。下面将详细讨论Struts2中的文件上传与下载实现,并涵盖相关知识点。 **1. 文件上传** 在Struts2...
在本实例中,我们将探讨如何使用QT通过HTTP协议来实现图片的实时上传、下载及显示。这涉及到QT中的网络模块,包括QNetworkAccessManager、QNetworkRequest、QNetworkReply等关键类。 首先,要实现图片的HTTP上传,...
在本项目中,我们将探讨如何使用JavaScript(JS)与Koa2框架来构建一个支持文件上传、下载和预览功能的简易应用。这个项目特别适合初学者,因为它的实现过程清晰明了,易于理解。 首先,Koa2是Node.js平台上的一个...
通用文件上传下载接口使用说明 通用文件上传下载接口使用说明是指在 JAVA 环境下的一种文件上传下载接口,主要提供文件上传、下载和删除三个主要功能。下面将对这三个主要功能进行详细的解释: 文件上传 文件上传...
在"struts2上传下载项目"中,我们可以深入理解如何利用Struts2实现文件的上传与下载功能。 首先,上传功能在Web应用中十分常见,比如用户在注册时上传头像,或者提交文档等。在Struts2中,我们主要借助`struts2-...
Apache 文件上传与文件下载是Web开发中的常见功能,涉及到服务器端处理客户端发送的文件数据以及提供文件给用户下载。本案例将详细讲解如何在Apache环境下实现文件上传和下载。 首先,我们来看`web.xml`的配置部分...
在基于Struts2的文件上传下载功能中,它提供了处理用户上传文件和提供文件下载的服务。这个完整的源代码是实现这些功能的一个实例,经过测试确保了其正确性和可用性。 首先,我们要理解Struts2中的Action类。Action...
本文档主要介绍了使用GE_PLC上传下载程序的方法和步骤,涵盖了上传下载程序的准备工作、上传下载步骤、更换CPU、子站上传下载等方面的知识点。 一、上传下载程序的准备工作 在上传下载程序之前,需要准备好相关的...
C 语言实现的http文件上传下载服务 系统平台:windows 开发工具:vs2010 开发语言:C 程序为单线程,使用I/O多路复用实现并发 抽取libevent的最最最基础框架,自己封装event 使用BSD tree.h的红黑树
文件上传和下载是Web应用程序中的核心功能之一,无论是用户向服务器提交个人资料、分享文档,还是从服务器获取资源,如软件更新、电子书籍等,都离不开这一操作。在这个过程中,前端与后端的交互以及数据的安全传输...
ApacheHTTP服务器的文件上传与下载功能配置.docx