2

I'm trying to upload a file using Java (HTTP Post):

HttpURLConnection conn = (HttpURLConnection) new URL(_uploadTarget).openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
conn.setDoOutput(true);
long fileLength = fileContentLength + tail.length();
String stringData = "--" + boundary + "\r\nContent-Disposition: form-data; name=\"metadata\"\r\n\r\n" + metadata + "\r\n" + "--" + boundary + "\r\nContent-Disposition: form-data; name=\"uploadfile\"; filename=\"" + fileName + "\"\r\nContent-Type: application/octet-stream; charset=UTF-8\r\nContent-Transfer-Encoding: binary\r\n" + "Content-length: " + fileLength + "\r\n\r\n";

long requestLength = stringData.length() + fileLength;
conn.setRequestProperty("Content-length", "" + requestLength);
conn.setFixedLengthStreamingMode((int) requestLength);
conn.connect();
DataOutputStream out = new DataOutputStream(conn.getOutputStream());

out.writeBytes(stringData);
out.flush();

int progress = 0;
int bytesRead = 0;
byte b[] = new byte[1024];
BufferedInputStream bufin = new BufferedInputStream(
        new FileInputStream(_file));
while ((bytesRead = bufin.read(b)) != -1) {
    out.write(b, 0, bytesRead);
    out.flush();
    progress += bytesRead;
}

out.writeBytes(tail);
out.flush();
out.close();

BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
StringBuilder sb = new StringBuilder();

while ((line = rd.readLine()) != null) {
    sb.append(line);
}

c# server:

public void ProcessRequest(HttpContext context) {
var uploadedFile = context.Request.Files[0]; // often throws exception with message: "Thread was being aborted."
}

This code works - some times. Hope someone can help.

2
  • And it never shows you any kind of .... exception ? Commented Jan 6, 2011 at 14:23
  • @Riduidel Code says "// often throws exception with message: "Thread was being aborted."" in the server side script. Commented Jan 6, 2011 at 14:25

2 Answers 2

3

Seeing that I don't know what errors/exception you're getting, since you're uploading file(s) using HttpURLConnection, then I would suggest reading:

Sign up to request clarification or add additional context in comments.

Comments

0

Although some crucial information is missing in your code example, I think your Java part is fine since it "works sometimes" and the exception is specific to C#.

I don't do C#, but Google reveals among others your Stackoverflow question the following link: http://www.debugging.com/bug/14721. Here's an extract of relevance:

Solved it by setting the Request execution timeout timer to a little higher value than 2minutes :)

If it's true that request processing takes that long, your solution may be to set that timeout higher.

1 Comment

You only need to configure timeout and request length in the web.config file, if you are uploading large files. I'm trying with files around 2 to 50 kb. However the other suggestion solved the issue. There's something wrong with the Java code I posted above. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.