C# 用Java上传文件

C# 用Java上传文件,c#,java,.net,httphandler,httpurlconnection,C#,Java,.net,Httphandler,Httpurlconnection,我正在尝试使用Java(HTTP Post)上传文件: HttpURLConnection conn = (HttpURLConnection) new URL(_uploadTarget).openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); conn.setDoOutput(tr

我正在尝试使用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);
}
public void ProcessRequest(HttpContext context) {
var uploadedFile = context.Request.Files[0]; // often throws exception with message: "Thread was being aborted."
}
c#服务器:

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);
}
public void ProcessRequest(HttpContext context) {
var uploadedFile = context.Request.Files[0]; // often throws exception with message: "Thread was being aborted."
}

这段代码有时有效。希望有人能提供帮助。

鉴于我不知道您遇到了什么错误/异常,因为您正在使用
HttpURLConnection
上载文件,我建议阅读:

  • 。只需向下滚动到上传文件

尽管您的代码示例中缺少一些关键信息,但我认为您的Java部分很好,因为它“有时会工作”,并且异常是特定于C的

我不做C#,但在其他人中,我显示了您的Stackoverflow问题的以下链接:。以下是一段相关摘录:

通过将请求执行超时计时器设置为略高于2分钟的值来解决此问题:)


如果请求处理确实需要那么长的时间,您的解决方案可能是将超时设置得更高。

而且它不会显示任何类型的。。。。异常?@Riduidel代码表示“/”经常抛出异常,并在服务器端脚本中显示消息:“线程被中止。”。如果上载大文件,只需在web.config文件中配置超时和请求长度。我正在尝试使用大约2到50KB的文件。然而,另一项建议解决了这个问题。我上面发布的Java代码有问题。:-)