gpt4 book ai didi

java - 无法在android中使用Content-Disposition上传照片

转载 作者:行者123 更新时间:2023-11-29 09:04:28 25 4
gpt4 key购买 nike

我正在尝试使用 Multipart/form-data 将图像上传到服务器,我必须在同一调用中发送一些文本和图像。当我只发送原始数据时,它工作正常,但是当我添加“图像上传部分”时它不起作用。下面是我的代码。

 public String upLoadPostMethod(List<NameValuePair> params, boolean isUploadingPhoto) {
NameValuePair nvp = params.get(1);
String args = nvp.getValue();
System.out.println(args);
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "********";
HttpsURLConnection conn;
String response = "";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
String mimeType = "image/jpeg";

try {
URL url = new URL(webServiceUrl);
conn = (HttpsURLConnection) url.openConnection();

conn.setDoInput(true);
conn.setDoOutput(true);
//conn.setChunkedStreamingMode(0);
// Use a post method.
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Cookie",
appStatus.getSharedStringValue(appStatus.AUTH_KEY));
System.out.println(appStatus
.getSharedStringValue(appStatus.AUTH_KEY));
conn.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);

DataOutputStream dos;
dos = new DataOutputStream(conn.getOutputStream());

// Send parameter #1
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"cmd\""
+ lineEnd + lineEnd);
dos.writeBytes("tsql" + lineEnd);
// Send parameter #2
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"args\""
+ lineEnd + lineEnd);
dos.writeBytes(args + lineEnd);
System.out.println("args======" + args);
if(isUploadingPhoto){

dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"file\";"
+"filename=\" 20130125_155208.jpg\"" +"Content-Type: "+mimeType +lineEnd+lineEnd);

FileInputStream fileInputStream = new FileInputStream(Environment.getExternalStorageDirectory().getPath()
+"/20130125_155208.jpg");

bytesAvailable = fileInputStream.available();

bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];

// read file and write it into form...

bytesRead = fileInputStream.read(buffer, 0, bufferSize);

while (bytesRead > 0)
{
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}

dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

fileInputStream.close();
}else {
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
}

dos.flush();
dos.close();

Scanner inStream = new Scanner(conn.getInputStream());
while (inStream.hasNextLine())
response += (inStream.nextLine());

System.out.println(response);
} catch (IOException e) {
e.printStackTrace();
}
return response;
}

我这里有什么地方做错了吗?

最佳答案

我猜你需要输入 encType="multipart/form-data"在你的<form>表单页面中的标签:示例代码如下,希望对您有所帮助:

因为我使用的是 Struts 1.3.8 并且我在 jsp 中有一个用户注册页面,用户必须在其中上传图像以及一些文本信息,如姓名、地址等....

<html:form action="/register?s=r" method="Post" enctype="multipart/form-data" onsubmit="return validateFob(this)">

<table width="65%" border="0" height="65%" bgcolor="beige">




.....
............
</table>

关于java - 无法在android中使用Content-Disposition上传照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15805131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com