/ Published in: Java
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
int maxBufferSize = 1*256*1024; int headerSize=89; int fileSize=0; int infoSize=0; "; int bytesRead, bytesAvailable, bufferSize; byte[] buffer; try { Log.i("MyAPP","Start uploading file: " + file[0]); // open a URL connection to the Servlet //Open a HTTP connection to the URL // Allow Inputs conn.setDoInput(true); // Allow Outputs conn.setDoOutput(true); // Don't use a cached copy. conn.setUseCaches(false); // Use a post method. fileSize=fileInputStream.available(); infoSize=fileSize + headerSize + file[0].length(); conn.setFixedLengthStreamingMode(infoSize); conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name='uploadedfile';filename='" + file[0] + "'" + lineEnd); dos.writeBytes(lineEnd); // create a buffer of maximum size bytesAvailable = fileInputStream.available(); buffer = new byte[bufferSize]; // read file and write it into form... bytesRead = fileInputStream.read(buffer, 0, bufferSize); int bytesSent=0; while (bytesRead > 0) { if (bytesSent > 0){ int pg=(bytesSent*100)/infoSize; publishProgress(pg); } bytesSent+=bufferSize; dos.write(buffer, 0, bufferSize); bytesAvailable = fileInputStream.available(); 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 fileInputStream.close(); dos.flush(); dos.close(); } { Log.e("MyAPP", "error: " + ex.getMessage(), ex); ret=1; } { Log.e("MyAPP", "error: " + ioe.getMessage(), ioe); ret=1; } try { String str; while (( str = inStream.readLine()) != null) { Log.i("MyAPP","Server Response"+str); } inStream.close(); } Log.e("MyAPP", "error: " + ioex.getMessage(), ioex); ret=1; } return ret; }