アップロードファイルのJava
-
26-09-2019 - |
質問
ようにしているファイルをアップロードするにはJavaを使用(HTTP)
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."
}
このコード作品の一部です。希望の人は助けることができる。
解決
を見るとどうなるかはわからない誤差/例外を取得しているからだアップロードファイルを使用 HttpURLConnection
, そ私はあなたが読む:
- BalusCの使用方法java.います。URLConnectionの火災対応HTTP要求?.直スクロールして、 ファイルのアップロード.
他のヒント
一部の重要な情報が欠けたコード例では、行きたいというJavaの部品が微細で作品"時の例外はC#.
いいスレッドセーフで、C#、も Google この中でその他のおStackoverflowの質問は、下記のリンク: http://www.debugging.com/bug/14721.こちらの抽出物との関連性:
解決を設定することで、その要求の実行のタイムアウトタイマーにとってどの値より大きな値2:)
の場合はtrue、それ以上の処理はこの長いソリューションが可能となりましたがセットのタイムアウト高い。
所属していません StackOverflow