使用Java上传文件

发布于 2024-10-10 19:55:47 字数 1819 浏览 5 评论 0原文

我正在尝试使用 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."
}

此代码有时有效。希望有人能帮忙。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流心雨 2024-10-17 19:55:47

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

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:

開玄 2024-10-17 19:55:47

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

我不使用 C#,但 Google 通过以下链接显示您的 Stackoverflow 问题:http://www.debugging .com/bug/14721。以下是相关性的摘录:

通过将请求执行超时计时器设置为比 2 分钟稍高的值来解决这个问题:)

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文