0
我想以编程方式上传文件到服务器而不是网站。我已成功访问该网站,并可以取消/解析不同的网页,但我无法上传文件。有问题的服务器使用apache tapestry进行剪纸。多部分后上传文件
这里是相关的Java代码:
private String uploadFile (String params, String filePath, String HTML) throws Exception {
String postUrl = getUploadUrl(HTML);
File fileToUpload = new File(filePath);
postUrl = "http://printing.**.ca:9191" + postUrl;
String random = "";
Random ran = new Random();
for (int i = 0; i < 28; i++) {
random = random + String.valueOf(ran.nextInt(9));
}
String boundry = "---------------------------" + random;
URL obj = new URL(postUrl);
connection = (HttpURLConnection)obj.openConnection();
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundry);
connection.setRequestProperty("Host", "printing.**.ca:9191");
connection.setRequestProperty("User-Agent", USER_AGENT);
connection
.setRequestProperty("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
connection.setRequestProperty("Referer",
"http://printing.**.ca:9191/app");
connection.setRequestProperty("Connection", "keep-alive");
for (String cookie : this.cookies) {
connection.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
}
connection.setDoInput(true);
connection.setDoOutput(true);
String fileType = getFileType(fileToUpload);
PrintWriter writer = null;
try {
writer = new PrintWriter(new OutputStreamWriter(connection.getOutputStream()));
writer.println(boundry);
writer.println("Content-Disposition: " + "form-data; " + "name=\"file[]\"; " + "filename=\"" + fileToUpload.getName() + "\"");
writer.println("Content-Type: " + fileType);
writer.println();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileToUpload)));
for (String line; (line = reader.readLine()) != null;) {
writer.print(line);
}
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {}
}
}
writer.println(boundry + "--");
} finally {
if (writer != null) {
writer.close();
}
}
int responseCode = connection.getResponseCode();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuffer buffer = new StringBuffer();
String inputLine;
while((inputLine = reader.readLine()) != null) {
buffer.append(inputLine);
}
reader.close();
return null;
我用丝鲨比较成功的请求,我的失败的请求,我不能确定在哪里我已经错了。
这里是成功的请求:
POST /upload/3229 HTTP/1.1
Host: printing.**.ca:9191
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:33.0) Gecko/20100101 Firefox/33.0
Accept: application/json
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
X-Requested-With: XMLHttpRequest
Referer: http://printing.**.ca:9191/app
Content-Length: 27682
Content-Type: multipart/form-data; boundary=---------------------------8555452061745260577115383266
Cookie: JSESSIONID=1w7usft10tnew
Connection: keep-alive
Pragma: no-cache
-----------------------------8555452061745260577115383266
Content-Disposition: form-data; name="file[]"; filename="hello.xls"
Content-Type: application/vnd.ms-excel
**data***
-----------------------------8555452061745260577115383266--
这个返回200/OK 然后触发关:
POST /app HTTP/1.1
Host: printing.**.ca:9191
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:33.0) Gecko/20100101 Firefox/33.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://printing.**.ca:9191/app
Cookie: org.apache.tapestry.locale=en; JSESSIONID=1w7usft10tnew
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 66
service=direct%2F1%2FUserWebPrintUpload%2F%24Form%240&sp=S1&Form1=
这是从我的请求的数据包:
POST /upload/3239 HTTP/1.1
Content-Type: multipart/form-data; boundary=---------------------------6735033783816657573427817664
User-Agent: Mozilla/5.0
Accept: application/json
Accept-Language: en-US,en;q=0.5
Referer: http://printing.**.ca:9191/app
X-Requested-With: XMLHttpRequest
Cache-Control: no-cache
Pragma: no-cache
Host: printing.**.ca:9191
Connection: keep-alive
Content-Length: 46431
Cookie: JSESSIONID=1i2ym6tnouzkw;
---------------------------6735033783816657573427817664
Content-Disposition: form-data; name="file[]"; filename="hello.xls"
Content-Type: application/vnd.ms-excel
**data*
---------------------------6735033783816657573427817664--
然后我得到一个200/ok 火这个:
POST /app HTTP/1.1
User-Agent: Mozilla/5.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Referer: http://printing.**.ca:9191/app
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache
Pragma: no-cache
Host: printing.**.ca:9191
Connection: keep-alive
Content-Length: 66
Cookie: JSESSIONID=1i2ym6tnouzkw;
service=direct%2F1%2FUserWebPrintUpload%2F%24Form%240&sp=S1&Form1=
并获取上传文件响应的错误。对于/ upload/3239,我抓取了包含上载文件格式的HTML。该网站也使用Dropzone.js,但有能力以简单的上传形式回退。 对于会话cookie,字符“;”在所有其他请求上发送并且没有任何失败。我有权访问网站,但似乎无法正确上传文件。
想法??