📄 fileuploadthreadhttp.java
字号:
// Get the GET parameters from the URL and convert them to
// post form params
ByteArrayEncoder formParams = getFormParamsForPostRequest(url);
contentLength += formParams.getEncodedLength();
this.connectionHelper.append(
"Content-Type: multipart/form-data; boundary=").append(
this.connectionHelper.getBoundary().substring(2)).append(
"\r\n");
this.connectionHelper.append("Content-Length: ").append(
String.valueOf(contentLength)).append("\r\n");
// Blank line (end of header)
this.connectionHelper.append("\r\n");
// formParams are not really part of the main header, but we add
// them here anyway. We write directly into the
// ByteArrayOutputStream, as we already encoded them, to get the
// encoded length. We need to flush the writer first, before
// directly writing to the ByteArrayOutputStream.
this.connectionHelper.append(formParams);
// Let's call the server
this.connectionHelper.sendRequest();
// Debug output: always called, so that the debug file is correctly
// filled.
this.uploadPolicy.displayDebug("=== main header (len="
+ this.connectionHelper.getByteArrayEncoder()
.getEncodedLength()
+ "):\n"
+ quoteCRLF(this.connectionHelper.getByteArrayEncoder()
.getString()), 70);
this.uploadPolicy.displayDebug("=== main header end", 70);
} catch (IOException e) {
throw new JUploadIOException(e);
} catch (IllegalArgumentException e) {
throw new JUploadException(e);
}
}
// ////////////////////////////////////////////////////////////////////////////////////
// /////////////////////// PRIVATE METHODS
// ////////////////////////////////////////////////////////////////////////////////////
/**
* Returns the header for this file, within the http multipart body.
*
* @param index Index of the file in the array that contains all files to
* upload.
* @param bound The boundary that separate files in the http multipart post
* body.
* @param chunkPart The numero of the current chunk (from 1 to n)
* @return The encoded header for this file. The {@link ByteArrayEncoder} is
* closed within this method.
* @throws JUploadException
*/
private final ByteArrayEncoder getFileHeader(int index, String bound,
int chunkPart) throws JUploadException {
String filenameEncoding = this.uploadPolicy.getFilenameEncoding();
String mimetype = this.filesToUpload[index].getMimeType();
String uploadFilename = this.filesToUpload[index]
.getUploadFilename(index);
ByteArrayEncoder bae = new ByteArrayEncoderHTTP(this.uploadPolicy,
bound);
// We'll encode the output stream into UTF-8.
String form = this.uploadPolicy.getFormdata();
if (null != form) {
bae.appendFormVariables(form);
}
// We ask the current FileData to add itself its properties.
this.filesToUpload[index].appendFileProperties(bae);
// boundary.
bae.append(bound).append("\r\n");
// Content-Disposition.
bae.append("Content-Disposition: form-data; name=\"");
bae.append(this.filesToUpload[index].getUploadName(index)).append(
"\"; filename=\"");
if (filenameEncoding == null) {
bae.append(uploadFilename);
} else {
try {
this.uploadPolicy.displayDebug("Encoded filename: "
+ URLEncoder.encode(uploadFilename, filenameEncoding),
70);
bae.append(URLEncoder.encode(uploadFilename, filenameEncoding));
} catch (UnsupportedEncodingException e) {
this.uploadPolicy
.displayWarn(e.getClass().getName() + ": "
+ e.getMessage()
+ " (in UploadFileData.getFileHeader)");
bae.append(uploadFilename);
}
}
bae.append("\"\r\n");
// Line 3: Content-Type.
bae.append("Content-Type: ").append(mimetype).append("\r\n");
// An empty line to finish the header.
bae.append("\r\n");
// The ByteArrayEncoder is now filled.
bae.close();
return bae;
}// getFileHeader
/**
* Construction of the head for each file.
*
* @param bound The String boundary between the post data in the HTTP
* request.
* @throws JUploadException
*/
private final void setAllHead(String bound) throws JUploadException {
this.heads = new ByteArrayEncoder[this.filesToUpload.length];
for (int i = 0; i < this.filesToUpload.length; i++) {
this.heads[i] = getFileHeader(i, bound, -1);
}
}
/**
* Construction of the tail for each file.
*
* @param bound Current boundary, to apply for these tails.
*/
private final void setAllTail(String bound) throws JUploadException {
this.tails = new ByteArrayEncoder[this.filesToUpload.length];
for (int i = 0; i < this.filesToUpload.length; i++) {
// We'll encode the output stream into UTF-8.
ByteArrayEncoder bae = new ByteArrayEncoderHTTP(this.uploadPolicy,
bound);
bae.append("\r\n");
bae.appendTextProperty("md5sum[]", this.filesToUpload[i].getMD5());
// The last tail gets an additional "--" in order to tell the
// server we have finished.
if (i == this.filesToUpload.length - 1) {
bae.append(bound).append("--\r\n");
}
// Let's store this tail.
bae.close();
this.tails[i] = bae;
}
}
/**
* Converts the parameters in GET form to post form
*
* @param url the <code>URL</code> containing the query parameters
* @return the parameters in a string in the correct form for a POST request
* @throws JUploadIOException
*/
private final ByteArrayEncoder getFormParamsForPostRequest(final URL url)
throws JUploadIOException {
// Use a string buffer
// We'll encode the output stream into UTF-8.
ByteArrayEncoder bae = new ByteArrayEncoderHTTP(this.uploadPolicy,
this.connectionHelper.getBoundary());
// Get the query string
String query = url.getQuery();
if (null != query) {
// Split this into parameters
HashMap<String, String> requestParameters = new HashMap<String, String>();
String[] paramPairs = query.split("&");
String[] oneParamArray;
// Put the parameters correctly to the Hashmap
for (String param : paramPairs) {
if (param.contains("=")) {
oneParamArray = param.split("=");
if (oneParamArray.length > 1) {
// There is a value for this parameter
requestParameters.put(oneParamArray[0],
oneParamArray[1]);
} else {
// There is no value for this parameter
requestParameters.put(oneParamArray[0], "");
}
}
}
// Now add one multipart segment for each
for (String key : requestParameters.keySet())
bae.appendTextProperty(key, requestParameters.get(key));
}
// Return the body content
bae.close();
return bae;
}// getFormParamsForPostRequest
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -