【发布时间】:2021-03-29 20:12:00
【问题描述】:
您好我正在尝试使用多部分表单数据 HttpURLConnection java 上传文件和文本,下面的代码示例:
String boundary = UUID.randomUUID().toString();
final String LINE_FEED = "\r\n";
OutputStream outputStream;
PrintWriter writer;
URL url_upload = new URL(url_api );
HttpURLConnection conn = (HttpURLConnection) url_upload.openConnection();
conn.setUseCaches(false);
conn.setDoOutput(true); // indicates POST method
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty( "Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("User-Agent", "Android Multipart HTTP Client 1.0");
conn.setRequestProperty("Connection", "Keep-Alive");
outputStream = conn.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(outputStream, "UTF-8"), true);
//DataOutputStream request = new DataOutputStream(conn.getOutputStream());
//Username
writer.append("--").append(boundary).append(LINE_FEED);
writer.append("Content-Disposition: form-data; name=\"" + "Username" + "\"")
.append(LINE_FEED);
writer.append("Content-Type: text/plain; charset=").append("UTF-8").append(
LINE_FEED);
writer.append(LINE_FEED);
writer.append(Username).append(LINE_FEED);
writer.flush();
//Password
writer.append("--").append(boundary).append(LINE_FEED);
writer.append("Content-Disposition: form-data; name=\"" + "Password" + "\"")
.append(LINE_FEED);
writer.append("Content-Type: text/plain; charset=").append("UTF-8").append(
LINE_FEED);
writer.append(LINE_FEED);
writer.append(Password);
writer.flush();
writer.append(LINE_FEED);
if(Food_image_file_name != null)
{
Log.i("File:", String.valueOf(file1));
writer.append("--").append(boundary).append(LINE_FEED);
writer.append("Content-Disposition: form-data; name=\"" + "file1" + "\"; filename=\"").append(file1.getName()).append("\"")
.append(LINE_FEED);
writer.append("Content-Type: ").append(URLConnection.guessContentTypeFromName(file1.getName()))
.append(LINE_FEED);
writer.append("Content-Transfer-Encoding: binary");
writer.append(LINE_FEED);
writer.flush();
writer.append(LINE_FEED);
FileInputStream inputStream = new FileInputStream(String.valueOf(file1.toPath()));
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
inputStream.close();
writer.append(LINE_FEED);
writer.flush();
writer.append(LINE_FEED).flush();
writer.append("--").append(boundary).append("--").append(LINE_FEED);
writer.close();
其他细节: I/文件:: /storage/emulated/0/DCIM/100ANDRO/DSC_0117.JPG I/Api 响应:400,错误请求
注意: 我从邮递员测试了服务器,它与邮递员一起工作没有问题,从邮递员成功上传到服务器的文件。
此链接是我用来读取请求的 Web api 示例的另一篇文章:link_here
更新: 我不得不稍微修改我的 web api,因为我没有正确使用,代码现在可以正常工作了
I/File:: /storage/emulated/0/Pictures/Viber/IMG-8bf36fec3b3c01df23c7faf763e2eac7-V.jpg
I/Boundary: ****
I/Image Content type: image/jpeg
在 Blob 图像 Azure 中,我看到图像不可见,它已损坏。
所以我的上一个问题是写图像,下面的正确代码,现在就像一个魅力;):
writer.append("--").append(boundary).append(LINE_FEED);
writer.append("Content-Disposition: form-data; name=\"" + "file1" + "\"; filename=\"").append(file1.getName()).append("\"")
.append(LINE_FEED);
writer.append("Content-Type: ").append(mimeType)
.append(LINE_FEED);
writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
writer.append(LINE_FEED);
writer.flush();
【问题讨论】:
标签: java android image multipartform-data httpurlconnection