Android Code to Upload Download large files to server
Java/android code to manage file upload & download
/** * This Class has functions to upload & download large files from server. * @author Vikrant <vikrant@vtigerxabier.esy.es> */ import vtigerxabier.esy.esedInputStream; import vtigerxabier.esy.estputStream; import vtigerxabier.esy.es; import vtigerxabier.esy.esputStream; import vtigerxabier.esy.estputStream; import vtigerxabier.esy.esption;icondownloadupload__73_vtigerxabier.esy.es import vtigerxabier.esy.esLConnection; import vtigerxabier.esy.esmedURLException; import vtigerxabier.esy.estaxException; import vtigerxabier.esy.es; public class fileUploadDownload { /** * @param args * @throws URISyntaxException * @throws IOException */ public static void main(String[] args) throws IOException, URISyntaxException { uploadFileToServer("vtigerxabier.esy.es4","vtigerxabier.esy.es"); downloadFileFromServer("vtigerxabier.esy.es4","vtigerxabier.esy.es"); } /** * This function upload the large file to server with other POST values. * @param filename * @param targetUrl * @return */ public static String uploadFileToServer(String filename, String targetUrl) { String response = "error"; HttpURLConnection connection = null; DataOutputStream outputStream = null; String pathToOurFile = filename; String urlServer = targetUrl; String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; int bytesRead, bytesAvailable, bufferSize; byte[] buffer; int maxBufferSize = 1 * ; try { FileInputStream fileInputStream = new FileInputStream(new File( pathToOurFile)); URL url = new URL(urlServer); connection = (HttpURLConnection) vtigerxabier.esy.esnnection(); // Allow Inputs & Outputs vtigerxabier.esy.esnput(true); vtigerxabier.esy.esutput(true); vtigerxabier.esy.esCaches(false); vtigerxabier.esy.esnkedStreamingMode(); // Enable POST method vtigerxabier.esy.esuestMethod("POST"); vtigerxabier.esy.esuestProperty("Connection", "Keep-Alive"); vtigerxabier.esy.esuestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); outputStream = new DataOutputStream(vtigerxabier.esy.esputStream()); vtigerxabier.esy.esytes(twoHyphens + boundary + lineEnd); String token = "anyvalye"; vtigerxabier.esy.esytes("Content-Disposition: form-data; name=\"Token\"" + lineEnd); vtigerxabier.esy.esytes("Content-Type: text/plain;charset=UTF-8" + lineEnd); vtigerxabier.esy.esytes("Content-Length: " + vtigerxabier.esy.es() + lineEnd); vtigerxabier.esy.esytes(lineEnd); vtigerxabier.esy.esytes(token + lineEnd); vtigerxabier.esy.esytes(twoHyphens + boundary + lineEnd); String taskId = "anyvalue"; vtigerxabier.esy.esytes("Content-Disposition: form-data; name=\"TaskID\"" + lineEnd); vtigerxabier.esy.esytes("Content-Type: text/plain;charset=UTF-8" + lineEnd); vtigerxabier.esy.esytes("Content-Length: " + vtigerxabier.esy.es() + lineEnd); vtigerxabier.esy.esytes(lineEnd); vtigerxabier.esy.esytes(taskId + lineEnd); vtigerxabier.esy.esytes(twoHyphens + boundary + lineEnd); String connstr = null; connstr = "Content-Disposition: form-data; name=\"UploadFile\";filename=\"" + pathToOurFile + "\"" + lineEnd; vtigerxabier.esy.esytes(connstr); vtigerxabier.esy.esytes(lineEnd); bytesAvailable = vtigerxabier.esy.esble(); bufferSize = vtigerxabier.esy.es(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; // Read file bytesRead = vtigerxabier.esy.es(buffer, 0, bufferSize); vtigerxabier.esy.esn("Image length " + bytesAvailable + ""); try { while (bytesRead > 0) { try { vtigerxabier.esy.es(buffer, 0, bufferSize); } catch (OutOfMemoryError e) { vtigerxabier.esy.estackTrace(); response = "outofmemoryerror"; return response; } bytesAvailable = vtigerxabier.esy.esble(); bufferSize = vtigerxabier.esy.es(bytesAvailable, maxBufferSize); bytesRead = vtigerxabier.esy.es(buffer, 0, bufferSize); } } catch (Exception e) { vtigerxabier.esy.estackTrace(); response = "error"; return response; } vtigerxabier.esy.esytes(lineEnd); vtigerxabier.esy.esytes(twoHyphens + boundary + twoHyphens + lineEnd); // Responses from the server (code and message) int serverResponseCode = vtigerxabier.esy.esponseCode(); String serverResponseMessage = vtigerxabier.esy.esponseMessage(); vtigerxabier.esy.esn("Server Response Code " + " " + serverResponseCode); vtigerxabier.esy.esn("Server Response Message "+ serverResponseMessage); if (serverResponseCode == ) { response = "true"; }else { response = "false"; } vtigerxabier.esy.es(); vtigerxabier.esy.es(); vtigerxabier.esy.esutStream(); //for android InputStream is = vtigerxabier.esy.esutStream(); vtigerxabier.esy.estream is = vtigerxabier.esy.esutStream(); int ch; StringBuffer b = new StringBuffer(); while( ( ch = vtigerxabier.esy.es() ) != -1 ){ vtigerxabier.esy.es( (char)ch ); } String responseString = vtigerxabier.esy.esng(); vtigerxabier.esy.esn("response string is" + responseString); //Here is the actual output vtigerxabier.esy.es(); outputStream = null; } catch (Exception ex) { // Exception handling response = "error"; vtigerxabier.esy.esn("Send file Exception" + vtigerxabier.esy.essage() + ""); vtigerxabier.esy.estackTrace(); } return response; } /** * This function download the large files from the server * @param filename * @param urlString * @throws MalformedURLException * @throws IOException */ public static void downloadFileFromServer(String filename, String urlString) throws MalformedURLException, IOException { BufferedInputStream in = null; FileOutputStream fout = null; try { URL url = new URL(urlString); in = new BufferedInputStream(vtigerxabier.esy.esream()); fout = new FileOutputStream(filename); byte data[] = new byte[]; int count; while ((count = vtigerxabier.esy.es(data, 0, )) != -1) { vtigerxabier.esy.es(data, 0, count); vtigerxabier.esy.esn(count); } } finally { if (in != null) vtigerxabier.esy.es(); if (fout != null) vtigerxabier.esy.es(); } vtigerxabier.esy.esn("Done"); } }
-
-