未接收多部分 HttpUrlConnection 数据
Multipart HttpUrlConnection data not receiving
我最近 2 天到处都在想。我的问题是我使用 multipart/form-data 发送多个文件,其中包含一些 text/plain 字段。
问题是当我使用 HTTPCLient
发送数据时它工作正常但是当我尝试使用 HTTPURLConnection
发送数据时,服务器没有收到任何东西,下面是我的 MultipartUtility
,
public class MultipartUtils extends NetworkUtility
{
private static final String END_REQUEST = "--";
private String mBoundary;
public MultipartUtils()
{
mBoundary = END_REQUEST + "quAxBSd";
}
public HttpURLConnection getUrlConnection(String URL, String httpMethod,
String contenttype, String boundry) throws Exception
{
URL url = new URL(URL);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
if (httpMethod.equalsIgnoreCase(HTTP_GET) == false)
urlConnection.setDoInput(true);
else
urlConnection.setDoInput(false);
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestMethod(httpMethod);
if (contenttype.equalsIgnoreCase(APPLICATION_MULTIPART))
{
urlConnection.setRequestProperty("Connection", "Keep-Alive");
urlConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundry);
urlConnection.setRequestProperty("ENCTYPE", "multipart/form-data");
}
else
{
urlConnection.setRequestProperty("Content-Type", contenttype);
}
return urlConnection;
}
public String uploadImagesAddPost(Activity mContext, String URL, String jsonString, ArrayList<ImageListBean> mImageBeanList) throws Exception
{
HttpURLConnection httpURLConnection = getUrlConnection(URL, HTTP_POST, APPLICATION_MULTIPART, mBoundary);
httpURLConnection.connect();
DataOutputStream dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
PrintWriter writer = new PrintWriter(new OutputStreamWriter(dataOutputStream, UTF8),
true);
addJsonToPart(writer, jsonString);
for (int i = 0; i < mImageBeanList.size(); i++)
{
try
{
byte[] imageByteArray = {};
Uri imageUri = mImageBeanList.get(i).getmUri();
String imagePath = ImageCaputureUtility.getPath(imageUri, mContext);
if (!imagePath.equals(""))
{
if (mImageBeanList.get(i).getmType().equalsIgnoreCase(MellTooConstants.IMG))
{
//For img
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, outputStream);
imageByteArray = outputStream.toByteArray();
addFileAsByte(dataOutputStream, "imageview" + (i + 1), imageByteArray, ("imageview" + (i + 1)) + ".jpeg", IMAGE_JPEG);
}
else
{
//For video
/* Uploading thumb*/
Bitmap bitmap = UtilsMellToo.createThumb(imageUri, mContext);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, outputStream);
imageByteArray = outputStream.toByteArray();
addFileAsByte(dataOutputStream, "imageview4", imageByteArray, "imageview4" + ".jpeg", IMAGE_JPEG);
/* Uploading video*/
imageByteArray = MellTooUtil.readFileToByteArray(new File(imagePath));
addFileAsByte(dataOutputStream, "video", imageByteArray, "video" + (i + 1) + ".mp4", VIDEO_MP4);
}
}
else
{
//No need to upload data
}
}
catch (Exception e)
{
e.printStackTrace();
}
if (i + 1 != mImageBeanList.size())
writer.append(mBoundary).append(CHANGE_LINE);
}
writer.append(mBoundary + END_REQUEST);
writer.flush();
return getResponse(httpURLConnection);
}
private void addJsonToPart(PrintWriter writer, String text)
{
writer.append(mBoundary).append(CHANGE_LINE);
writer.append(CONTENT_DISPOSITION + FORM_DATA + NAME + "\"formstring\"").append(CHANGE_LINE);
writer.append(CONTENT_TYPE + PLAIN_TEXT + CHARSET + UTF8).append(CHANGE_LINE);
writer.append(CONTENT_TRANSFER_ENCODING + "8bit").append(CHANGE_LINE);
writer.append(text).append(CHANGE_LINE);
writer.flush();
}
public void addFileAsByte(DataOutputStream outputStream, String fieldName, byte[] imageByteArray, String fileName, String contentType) throws IOException
{
PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, UTF8),
true);
writer.append(mBoundary).append(CHANGE_LINE);
writer.append(CONTENT_DISPOSITION + FORM_DATA + NAME + "\"" + fieldName + "\";" + FILE_NAME + "\"" + fileName + "\"").append(CHANGE_LINE);
writer.append(CONTENT_TYPE + contentType).append(CHANGE_LINE);
writer.append(CONTENT_TRANSFER_ENCODING + BINARY).append(CHANGE_LINE);
writer.flush();
InputStream inputStream = new ByteArrayInputStream(imageByteArray);
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.writeBytes(CHANGE_LINE);
outputStream.flush();
inputStream.close();
}
}
下面是方法,我是如何使用这个class,
jsonResponseString = new MultipartUtils()
.uploadImagesAddPost(mContext, AppConstants.BASE_URL + AppConstants.SAVE_POST_URL,
mJsonString, mImageList);
下面是我的ASP这边,
HttpContextWrapper.Request.Form["formstring"]; //This is returning null
请帮我解决这个问题...!!!
提前致谢
以下是我的要求,
经过大约 4 天的努力,我发现问题出在边界和请求中的新行中......!
文本和图像部分之间应该有一个边界和一个空行,我没有使用它。空行将 header 与 multipart/form-data
请求的每个部分的正文分开...!
我最近 2 天到处都在想。我的问题是我使用 multipart/form-data 发送多个文件,其中包含一些 text/plain 字段。
问题是当我使用 HTTPCLient
发送数据时它工作正常但是当我尝试使用 HTTPURLConnection
发送数据时,服务器没有收到任何东西,下面是我的 MultipartUtility
,
public class MultipartUtils extends NetworkUtility
{
private static final String END_REQUEST = "--";
private String mBoundary;
public MultipartUtils()
{
mBoundary = END_REQUEST + "quAxBSd";
}
public HttpURLConnection getUrlConnection(String URL, String httpMethod,
String contenttype, String boundry) throws Exception
{
URL url = new URL(URL);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
if (httpMethod.equalsIgnoreCase(HTTP_GET) == false)
urlConnection.setDoInput(true);
else
urlConnection.setDoInput(false);
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestMethod(httpMethod);
if (contenttype.equalsIgnoreCase(APPLICATION_MULTIPART))
{
urlConnection.setRequestProperty("Connection", "Keep-Alive");
urlConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundry);
urlConnection.setRequestProperty("ENCTYPE", "multipart/form-data");
}
else
{
urlConnection.setRequestProperty("Content-Type", contenttype);
}
return urlConnection;
}
public String uploadImagesAddPost(Activity mContext, String URL, String jsonString, ArrayList<ImageListBean> mImageBeanList) throws Exception
{
HttpURLConnection httpURLConnection = getUrlConnection(URL, HTTP_POST, APPLICATION_MULTIPART, mBoundary);
httpURLConnection.connect();
DataOutputStream dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
PrintWriter writer = new PrintWriter(new OutputStreamWriter(dataOutputStream, UTF8),
true);
addJsonToPart(writer, jsonString);
for (int i = 0; i < mImageBeanList.size(); i++)
{
try
{
byte[] imageByteArray = {};
Uri imageUri = mImageBeanList.get(i).getmUri();
String imagePath = ImageCaputureUtility.getPath(imageUri, mContext);
if (!imagePath.equals(""))
{
if (mImageBeanList.get(i).getmType().equalsIgnoreCase(MellTooConstants.IMG))
{
//For img
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, outputStream);
imageByteArray = outputStream.toByteArray();
addFileAsByte(dataOutputStream, "imageview" + (i + 1), imageByteArray, ("imageview" + (i + 1)) + ".jpeg", IMAGE_JPEG);
}
else
{
//For video
/* Uploading thumb*/
Bitmap bitmap = UtilsMellToo.createThumb(imageUri, mContext);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, outputStream);
imageByteArray = outputStream.toByteArray();
addFileAsByte(dataOutputStream, "imageview4", imageByteArray, "imageview4" + ".jpeg", IMAGE_JPEG);
/* Uploading video*/
imageByteArray = MellTooUtil.readFileToByteArray(new File(imagePath));
addFileAsByte(dataOutputStream, "video", imageByteArray, "video" + (i + 1) + ".mp4", VIDEO_MP4);
}
}
else
{
//No need to upload data
}
}
catch (Exception e)
{
e.printStackTrace();
}
if (i + 1 != mImageBeanList.size())
writer.append(mBoundary).append(CHANGE_LINE);
}
writer.append(mBoundary + END_REQUEST);
writer.flush();
return getResponse(httpURLConnection);
}
private void addJsonToPart(PrintWriter writer, String text)
{
writer.append(mBoundary).append(CHANGE_LINE);
writer.append(CONTENT_DISPOSITION + FORM_DATA + NAME + "\"formstring\"").append(CHANGE_LINE);
writer.append(CONTENT_TYPE + PLAIN_TEXT + CHARSET + UTF8).append(CHANGE_LINE);
writer.append(CONTENT_TRANSFER_ENCODING + "8bit").append(CHANGE_LINE);
writer.append(text).append(CHANGE_LINE);
writer.flush();
}
public void addFileAsByte(DataOutputStream outputStream, String fieldName, byte[] imageByteArray, String fileName, String contentType) throws IOException
{
PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, UTF8),
true);
writer.append(mBoundary).append(CHANGE_LINE);
writer.append(CONTENT_DISPOSITION + FORM_DATA + NAME + "\"" + fieldName + "\";" + FILE_NAME + "\"" + fileName + "\"").append(CHANGE_LINE);
writer.append(CONTENT_TYPE + contentType).append(CHANGE_LINE);
writer.append(CONTENT_TRANSFER_ENCODING + BINARY).append(CHANGE_LINE);
writer.flush();
InputStream inputStream = new ByteArrayInputStream(imageByteArray);
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.writeBytes(CHANGE_LINE);
outputStream.flush();
inputStream.close();
}
}
下面是方法,我是如何使用这个class,
jsonResponseString = new MultipartUtils()
.uploadImagesAddPost(mContext, AppConstants.BASE_URL + AppConstants.SAVE_POST_URL,
mJsonString, mImageList);
下面是我的ASP这边,
HttpContextWrapper.Request.Form["formstring"]; //This is returning null
请帮我解决这个问题...!!! 提前致谢
以下是我的要求,
经过大约 4 天的努力,我发现问题出在边界和请求中的新行中......!
文本和图像部分之间应该有一个边界和一个空行,我没有使用它。空行将 header 与 multipart/form-data
请求的每个部分的正文分开...!