📄 fileuploadthreadhttp.java
字号:
//
// $Id: FileUploadThreadHTTP.java 612 2009-02-16 11:42:50Z etienne_sf $
//
// jupload - A file upload applet.
// Copyright 2007 The JUpload Team
//
// Created: 2007-03-07
// Creator: etienne_sf
// Last modified: $Date: 2009-02-16 12:42:50 +0100 (lun., 16 févr. 2009) $
//
// This program is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation; either version 2 of the License, or (at your option) any later
// version. This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details. You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation, Inc.,
// 675 Mass Ave, Cambridge, MA 02139, USA.
package wjhk.jupload2.upload;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import wjhk.jupload2.exception.JUploadException;
import wjhk.jupload2.exception.JUploadIOException;
import wjhk.jupload2.policies.UploadPolicy;
import wjhk.jupload2.upload.helper.ByteArrayEncoder;
import wjhk.jupload2.upload.helper.ByteArrayEncoderHTTP;
import wjhk.jupload2.upload.helper.HTTPConnectionHelper;
/**
* This class implements the file upload via HTTP POST request.
*
* @author etienne_sf
* @version $Revision: 612 $
*/
public class FileUploadThreadHTTP extends DefaultFileUploadThread {
/**
* The current connection helper. No initialization now: we need to wait for
* the startRequest method, to have all needed information.
*/
private HTTPConnectionHelper connectionHelper = null;
/**
* local head within the multipart post, for each file. This is
* precalculated for all files, in case the upload is not chunked. The heads
* length are counted in the total upload size, to check that it is less
* than the maxChunkSize. tails are calculated once, as they depend not of
* the file position in the upload.
*/
private ByteArrayEncoder heads[] = null;
/**
* same as heads, for the ... tail in the multipart post, for each file. But
* tails depend on the file position (the boundary is added to the last
* tail). So it's to be calculated for each upload.
*/
private ByteArrayEncoder tails[] = null;
/**
* Creates a new instance.
*
* @param uploadPolicy The policy to be applied.
* @param fileUploadManagerThread
*/
public FileUploadThreadHTTP(UploadPolicy uploadPolicy,
FileUploadManagerThread fileUploadManagerThread) {
super(uploadPolicy, fileUploadManagerThread);
this.uploadPolicy.displayDebug(" Using " + this.getClass().getName(),
30);
uploadPolicy.displayDebug("Upload done by using the "
+ getClass().getName() + " class", 30);
// Name the thread (useful for debugging)
setName("FileUploadThreadHTTP");
this.connectionHelper = new HTTPConnectionHelper(uploadPolicy);
}
/** @see DefaultFileUploadThread#beforeRequest() */
@Override
void beforeRequest() throws JUploadException {
setAllHead(this.connectionHelper.getBoundary());
setAllTail(this.connectionHelper.getBoundary());
}
/** @see DefaultFileUploadThread#getAdditionnalBytesForUpload(int) */
@Override
long getAdditionnalBytesForUpload(int index) throws JUploadIOException {
return this.heads[index].getEncodedLength()
+ this.tails[index].getEncodedLength();
}
/** @see DefaultFileUploadThread#afterFile(int) */
@Override
void afterFile(int index) throws JUploadIOException {
this.connectionHelper.append(this.tails[index].getEncodedByteArray());
this.uploadPolicy.displayDebug("--- filetail start (len="
+ this.tails[index].getEncodedLength() + "):", 70);
this.uploadPolicy.displayDebug(
quoteCRLF(this.tails[index].getString()), 70);
this.uploadPolicy.displayDebug("--- filetail end", 70);
}
/** @see DefaultFileUploadThread#beforeFile(int) */
@Override
void beforeFile(int index) throws JUploadException {
// heads[i] contains the header specific for the file, in the multipart
// content.
// It is initialized at the beginning of the run() method. It can be
// override at the beginning of this loop, if in chunk mode.
try {
this.connectionHelper.append(this.heads[index]
.getEncodedByteArray());
// Debug output: always called, so that the debug file is correctly
// filled.
this.uploadPolicy.displayDebug("--- fileheader start (len="
+ this.heads[index].getEncodedLength() + "):", 70);
this.uploadPolicy.displayDebug(quoteCRLF(this.heads[index]
.getString()), 70);
this.uploadPolicy.displayDebug("--- fileheader end", 70);
} catch (Exception e) {
throw new JUploadException(e);
}
}
/** @see DefaultFileUploadThread#cleanAll() */
@Override
void cleanAll() throws JUploadException {
// Nothing to do in HTTP mode.
}
/** @see DefaultFileUploadThread#cleanRequest() */
@Override
void cleanRequest() throws JUploadException {
try {
this.connectionHelper.dispose();
} catch (JUploadIOException e) {
this.uploadPolicy.displayErr(this.uploadPolicy
.getString("errDuringUpload"), e);
throw e;
}
}
@Override
int finishRequest() throws JUploadException {
if (this.uploadPolicy.getDebugLevel() > 100) {
// Let's have a little time to check the upload messages written on
// the progress bar.
try {
Thread.sleep(400);
} catch (InterruptedException e) {
}
}
int status = this.connectionHelper.readHttpResponse();
setResponseMsg(this.connectionHelper.getResponseMsg());
setResponseBody(this.connectionHelper.getResponseBody());
return status;
}
/**
* @see DefaultFileUploadThread#getResponseBody()
* @Override String getResponseBody() { return
* this.sbHttpResponseBody.toString(); }
*/
/** @see DefaultFileUploadThread#getOutputStream() */
@Override
OutputStream getOutputStream() throws JUploadException {
return this.connectionHelper.getOutputStream();
}
/** @see DefaultFileUploadThread#startRequest(long, boolean, int, boolean) */
@Override
void startRequest(long contentLength, boolean bChunkEnabled, int chunkPart,
boolean bLastChunk) throws JUploadException {
try {
String chunkHttpParam = "jupart=" + chunkPart + "&jufinal="
+ (bLastChunk ? "1" : "0");
this.uploadPolicy.displayDebug("chunkHttpParam: " + chunkHttpParam,
30);
URL url = new URL(this.uploadPolicy.getPostURL());
// Add the chunking query params to the URL if there are any
if (bChunkEnabled) {
if (null != url.getQuery() && !"".equals(url.getQuery())) {
url = new URL(url.toExternalForm() + "&" + chunkHttpParam);
} else {
url = new URL(url.toExternalForm() + "?" + chunkHttpParam);
}
}
this.connectionHelper.initRequest(url, "POST", bChunkEnabled,
bLastChunk);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -