我想上传图片及其信息。我想为 android 使用multipart/form data json。我也看过这个 Stack Overflow 问题Android:How to upload .mp3 file and image to http server?
但这对我不起作用。
这是我的代码:
protected void doFileUpload(){
Intent i = getIntent();
String path = i.getStringExtra("pathimage");
HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
String existingFileName = path;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
String responseFromServer = "";
String urlString = Constants.url_create_store;
try
{
//------------------ CLIENT REQUEST
FileInputStream fileInputStream = new FileInputStream(new File(existingFileName) );
// open a URL connection to the Servlet
URL url = new URL(urlString);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
// Allow Inputs
conn.setDoInput(true);
// Allow Outputs
conn.setDoOutput(true);
// Don't use a cached copy.
conn.setUseCaches(false);
// Use a post method.
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
dos = new DataOutputStream( conn.getOutputStream() );
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + existingFileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
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);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// close streams
Log.e("Debug","File is written");
fileInputStream.close();
dos.flush();
dos.close();
}
catch (MalformedURLException ex)
{
Log.e("Debug", "error: " + ex.getMessage(), ex);
}
catch (IOException ioe)
{
Log.e("Debug", "error: " + ioe.getMessage(), ioe);
}
//------------------ read the SERVER RESPONSE
try {
inStream = new DataInputStream ( conn.getInputStream() );
String str;
while (( str = inStream.readLine()) != null)
{
Log.e("Debug","Server Response "+str);
}
inStream.close();
}
catch (IOException ioex){
Log.e("Debug", "error: " + ioex.getMessage(), ioex);
}
}
}
这是我的日志:
10-03 11:17:07.782: DEBUG/dalvikvm(119): GC_EXTERNAL_ALLOC freed 706 objects / 34184 bytes in 839ms
10-03 11:17:13.052: INFO/ActivityManager(67): Starting activity: Intent { cmp=shoop3.android.edu/.Activity3 (has extras) }
10-03 11:17:13.732: DEBUG/dalvikvm(468): GC_EXTERNAL_ALLOC freed 1652 objects / 91728 bytes in 187ms
10-03 11:17:15.162: INFO/ActivityManager(67): Displayed activity shoop3.android.edu/.Activity3: 1929 ms (total 1929 ms)
10-03 11:17:20.942: ERROR/Debug(468): File is written
10-03 11:17:24.802: ERROR/Debug(468): Server Response {"status":1}
10-03 11:17:25.272: ERROR/JSON Parser(468): Error parsing data org.json.JSONException: End of input at character 0 of
10-03 11:17:25.282: DEBUG/Create Response(468): {"status":1}
10-03 11:17:25.312: INFO/ActivityManager(67): Starting activity: Intent { cmp=shoop3.android.edu/.shoop3Activity }
10-03 11:17:26.825: INFO/ActivityManager(67): Displayed activity shoop3.android.edu/.shoop3Activity: 1349 ms (total 1349 ms)
10-03 11:19:24.372: DEBUG/SntpClient(67): request time failed: java.net.SocketException: Address family not supported by protocol