Monday, March 3, 2014

Ask questions HttpUrlConnection upload files


Has been added
Code: Address effective implementation of the DataOutputStream dos = new DataOutputStream (httpURLConnection.getOutputStream ()); when an exception is thrown, I do not know how else?
java.io.IOException
public void onFileItemClick (String filename)
{
String uploadUrl = "http://192.168.1.103:8080/uhonesty/uploadFile";
String end = "\ r \ n";
String twoHyphens = "-"; / / two hyphens
String boundary = "******"; / / delimiter string
try
{
URL url = new URL (uploadUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection ();
httpURLConnection.setDoInput (true);
httpURLConnection.setDoOutput (true);
httpURLConnection.setUseCaches (false);
httpURLConnection.setRequestMethod ("POST");
/ / Set the Http request header
httpURLConnection.setRequestProperty ("Connection", "Keep-Alive");
httpURLConnection.setRequestProperty ("Charset", "UTF-8");
/ / Must be specified in any string delimiter
the Content-Type request headerhttpURLConnection.setRequestProperty ("Content-Type", "multipart / form-data; boundary =" + boundary);

/ / Define data written to the stream, ready to upload files
DataOutputStream dos = new DataOutputStream (httpURLConnection.getOutputStream ()) ;/ / here throws, java.io.IOException
dos.writeBytes (twoHyphens + boundary + end);
/ / Set Information
associated with the uploaded filedos.writeBytes ("Content-Disposition: form-data; name = \" file \ "; filename = \" "
+ Filename.substring (filename.lastIndexOf ("/") + 1)
+ "\" "+ End);
dos.writeBytes (end);

FileInputStream fis = new FileInputStream (filename);
byte [] buffer = new byte [8192]; / / 8k
int count = 0;
/ / Read the contents of the folder, and write the OutputStream object
while ((count = fis.read (buffer))! = -1)
{
dos.write (buffer, 0, count);
}
fis.close ();
dos.writeBytes (end);
dos.writeBytes (twoHyphens + boundary + twoHyphens + end);
dos.flush ();
/ / Start reading the information coming from the server
InputStream is = httpURLConnection.getInputStream ();
InputStreamReader isr = new InputStreamReader (is, "utf-8");
BufferedReader br = new BufferedReader (isr);
String result = br.readLine ();

Toast.makeText (this, result, Toast.LENGTH_LONG) show ();.
dos.close ();
is.close ();
}
catch (Exception e)
{
setTitle (e.getMessage ());
}
}<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Version of android 4.1.2
Reply:
There is no master Dingding know why? Thank you.
Reply:
Available on the 2.3 machine. In 4.0 above are the exception.
Reply:
The network operations into a new thread to try. 2.3 4.0 One obvious difference is that with the network operator can not be placed in the main thread of the UI.
Reply:
4.0 All of the above should be placed in the network operating thread
Reply:
4.0 can not be placed in the main thread
Reply:
Consuming operation like this, you need to start a thread, can not be executed in the main thread (ui thread). 4.0 does not have this restriction in the past, but after 4.0 has, which is dnr

No comments:

Post a Comment