2

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# サーバー:

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

このコードは機能します - 時々。誰かが助けてくれることを願っています。

4

2 に答える 2

3

を使用してファイルをアップロードしているため、発生しているエラー/例外がわからないのでHttpURLConnection、次を読むことをお勧めします。

于 2011-01-06T14:33:30.287 に答える
0

コード例にはいくつかの重要な情報がありませんが、Javaの部分は「時々機能」し、例外はC#に固有であるため、問題ないと思います。

私はC#を実行しませんが、Googleは特にStackoverflowの質問を次のリンクで公開しています:http ://www.debugging.com/bug/14721 。関連性の抜粋は次のとおりです。

リクエスト実行タイムアウトタイマーを2分より少し高い値に設定することで解決しました:)

リクエストの処理にこれほど長い時間がかかることが事実である場合、解決策はそのタイムアウトをより高く設定することかもしれません。

于 2011-01-06T14:50:12.140 に答える