List of usage examples for java.net HttpURLConnection setChunkedStreamingMode
public void setChunkedStreamingMode(int chunklen)
From source file:org.wso2.msf4j.internal.router.HttpServerTest.java
@Test public void testUploadReject() throws Exception { HttpURLConnection urlConn = request("/test/v1/uploadReject", HttpMethod.POST, true); try {/* w w w .j a v a2 s .com*/ urlConn.setChunkedStreamingMode(1024); urlConn.getOutputStream().write("Rejected Content".getBytes(Charsets.UTF_8)); try { urlConn.getInputStream(); Assert.fail(); } catch (IOException e) { // Expect to get exception since server response with 400. Just drain the error stream. ByteStreams.toByteArray(urlConn.getErrorStream()); Assert.assertEquals(HttpResponseStatus.BAD_REQUEST.code(), urlConn.getResponseCode()); } } finally { urlConn.disconnect(); } }
From source file:com.drive.student.xutils.HttpUtils.java
/** * Server//from ww w . ja v a 2 s.co m * * @param urlStr * ? * @param serverFileName * ????? image.jpg * @param uploadFile * ? /sdcard/a.jpg */ public void uploadFile(String urlStr, String serverFileName, File uploadFile) { try { URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setDoInput(true); conn.setChunkedStreamingMode(1024 * 1024); conn.setRequestMethod("POST"); conn.setRequestProperty("X-Requested-With", "XMLHttpRequest"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("Charsert", "UTF-8"); conn.setRequestProperty("Content-Type", "multipart/form-data;file=" + uploadFile.getName()); conn.setRequestProperty("filename", uploadFile.getName()); OutputStream out = new DataOutputStream(conn.getOutputStream()); DataInputStream in = new DataInputStream(new FileInputStream(uploadFile)); int bytes = 0; byte[] bufferOut = new byte[1024]; while ((bytes = in.read(bufferOut)) != -1) { out.write(bufferOut, 0, bytes); } in.close(); out.flush(); out.close(); int response = conn.getResponseCode(); if (response == 200) { BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line = null; while ((line = reader.readLine()) != null) { System.out.println(line); } LogUtil.e("hxk", "upload file success-->>"); } else { LogUtil.e("hxk", "upload file fail-->> response = " + response); } } catch (Exception e) { e.printStackTrace(); LogUtil.e("hxk", "upload file fail-->>"); } }
From source file:com.amazonaws.http.UrlHttpClient.java
void configureConnection(HttpRequest request, HttpURLConnection connection) { // configure the connection connection.setConnectTimeout(config.getConnectionTimeout()); connection.setReadTimeout(config.getSocketTimeout()); // disable redirect and cache connection.setInstanceFollowRedirects(false); connection.setUseCaches(false);//from w w w . j a v a2s . co m // is streaming if (request.isStreaming()) { connection.setChunkedStreamingMode(0); } // configure https connection if (connection instanceof HttpsURLConnection) { final HttpsURLConnection https = (HttpsURLConnection) connection; // disable cert check /* * Commented as per https://support.google.com/faqs/answer/6346016. Uncomment for testing. if (System.getProperty(DISABLE_CERT_CHECKING_SYSTEM_PROPERTY) != null) { disableCertificateValidation(https); } */ if (config.getTrustManager() != null) { enableCustomTrustManager(https); } } }
From source file:org.apache.zeppelin.warp10.Warp10Interpreter.java
public Pair<InterpreterResult.Code, String> execRequest(String body) throws Exception { ///* www . j a va 2 s .c o m*/ // Execute the request on current url defined // String url = this.current_Url; url += "/exec"; URL obj = new URL(url); HttpURLConnection con = null; // // If HTTPS execute an HTTPS connection // if (url.startsWith("https")) { con = (HttpsURLConnection) obj.openConnection(); } else { con = (HttpURLConnection) obj.openConnection(); } //add request header con.setDoOutput(true); con.setDoInput(true); con.setRequestMethod("POST"); con.setChunkedStreamingMode(16384); con.connect(); // // Write the body in the request // OutputStream os = con.getOutputStream(); //GZIPOutputStream out = new GZIPOutputStream(os); PrintWriter pw = new PrintWriter(os); pw.println(body); pw.close(); StringBuffer response = new StringBuffer(); Pair<InterpreterResult.Code, String> resultPair = null; // // If answer equals 200 parse result stream, otherwise error Stream // if (200 == con.getResponseCode()) { BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { response.append(inputLine); } resultPair = new Pair<InterpreterResult.Code, String>(InterpreterResult.Code.SUCCESS, response.toString()); in.close(); con.disconnect(); } else { String fillBackEnd = "Warp10"; if (this.isCzdBackend) { fillBackEnd = "CityzenData"; } String errorLine = "\"Error-Line\":" + con.getHeaderField("X-" + fillBackEnd + "-Error-Line"); String errorMsg = "\"Error-Message\":\"" + con.getHeaderField("X-" + fillBackEnd + "-Error-Message") + "\""; response.append("[{"); response.append(errorLine + ","); response.append(errorMsg); boolean getBody = (null == con.getContentType()); if (!getBody && !con.getContentType().startsWith("text/html")) { getBody = true; } if (getBody) { response.append(",\"Body\":\""); BufferedReader in = new BufferedReader(new InputStreamReader(con.getErrorStream())); String inputLine; while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); response.append("\""); } response.append("}]"); resultPair = new Pair<InterpreterResult.Code, String>(InterpreterResult.Code.ERROR, response.toString()); con.disconnect(); } // // Return the body message with its associated code (SUCESS or ERROR) // return resultPair; }
From source file:edu.stanford.junction.sample.partyware.ImgurUploadService.java
/** * This method uploads an image from the given uri. It does the uploading in * small chunks to make sure it doesn't over run the memory. * //ww w . ja v a2 s . co m * @param uri * image location * @return map containing data from interaction */ private String readPictureDataAndUpload(final Uri uri) { Log.i(this.getClass().getName(), "in readPictureDataAndUpload(Uri)"); try { final AssetFileDescriptor assetFileDescriptor = getContentResolver().openAssetFileDescriptor(uri, "r"); final int totalFileLength = (int) assetFileDescriptor.getLength(); assetFileDescriptor.close(); // Create custom progress notification mProgressNotification = new Notification(R.drawable.icon, getString(R.string.imgur_upload_in_progress), System.currentTimeMillis()); // set as ongoing mProgressNotification.flags |= Notification.FLAG_ONGOING_EVENT; // set custom view to notification mProgressNotification.contentView = generateProgressNotificationView(0, totalFileLength); //empty intent for the notification final Intent progressIntent = new Intent(); final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, progressIntent, 0); mProgressNotification.contentIntent = contentIntent; // add notification to manager mNotificationManager.notify(NOTIFICATION_ID, mProgressNotification); final InputStream inputStream = getContentResolver().openInputStream(uri); final String boundaryString = "Z." + Long.toHexString(System.currentTimeMillis()) + Long.toHexString((new Random()).nextLong()); final String boundary = "--" + boundaryString; final HttpURLConnection conn = (HttpURLConnection) (new URL("http://imgur.com/api/upload.json")) .openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-type", "multipart/form-data; boundary=\"" + boundaryString + "\""); conn.setUseCaches(false); conn.setDoInput(true); conn.setDoOutput(true); conn.setChunkedStreamingMode(CHUNK_SIZE); final OutputStream hrout = conn.getOutputStream(); final PrintStream hout = new PrintStream(hrout); hout.println(boundary); hout.println("Content-Disposition: form-data; name=\"key\""); hout.println("Content-Type: text/plain"); hout.println(); Random rng = new Random(); String key = API_KEYS[rng.nextInt(API_KEYS.length)]; hout.println(key); hout.println(boundary); hout.println("Content-Disposition: form-data; name=\"image\""); hout.println("Content-Transfer-Encoding: base64"); hout.println(); hout.flush(); { final Base64OutputStream bhout = new Base64OutputStream(hrout); final byte[] pictureData = new byte[READ_BUFFER_SIZE_BYTES]; int read = 0; int totalRead = 0; long lastLogTime = 0; while (read >= 0) { read = inputStream.read(pictureData); if (read > 0) { bhout.write(pictureData, 0, read); totalRead += read; if (lastLogTime < (System.currentTimeMillis() - PROGRESS_UPDATE_INTERVAL_MS)) { lastLogTime = System.currentTimeMillis(); Log.d(this.getClass().getName(), "Uploaded " + totalRead + " of " + totalFileLength + " bytes (" + (100 * totalRead) / totalFileLength + "%)"); //make a final version of the total read to make the handler happy final int totalReadFinal = totalRead; mHandler.post(new Runnable() { public void run() { mProgressNotification.contentView = generateProgressNotificationView( totalReadFinal, totalFileLength); mNotificationManager.notify(NOTIFICATION_ID, mProgressNotification); } }); } bhout.flush(); hrout.flush(); } } Log.d(this.getClass().getName(), "Finishing upload..."); // This close is absolutely necessary, this tells the // Base64OutputStream to finish writing the last of the data // (and including the padding). Without this line, it will miss // the last 4 chars in the output, missing up to 3 bytes in the // final output. bhout.close(); Log.d(this.getClass().getName(), "Upload complete..."); mProgressNotification.contentView.setProgressBar(R.id.UploadProgress, totalFileLength, totalRead, false); mNotificationManager.cancelAll(); } hout.println(boundary); hout.flush(); hrout.close(); inputStream.close(); Log.d(this.getClass().getName(), "streams closed, " + "now waiting for response from server"); final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); final StringBuilder rData = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { rData.append(line).append('\n'); } return rData.toString(); } catch (final IOException e) { Log.e(this.getClass().getName(), "Upload failed", e); } return null; }
From source file:com.maass.android.imgur_uploader.ImgurUpload.java
/** * This method uploads an image from the given uri. It does the uploading in * small chunks to make sure it doesn't over run the memory. * //from ww w .j av a2 s. c o m * @param uri * image location * @return map containing data from interaction */ private String readPictureDataAndUpload(final Uri uri) { Log.i(this.getClass().getName(), "in readPictureDataAndUpload(Uri)"); try { final AssetFileDescriptor assetFileDescriptor = getContentResolver().openAssetFileDescriptor(uri, "r"); final int totalFileLength = (int) assetFileDescriptor.getLength(); assetFileDescriptor.close(); // Create custom progress notification mProgressNotification = new Notification(R.drawable.icon, getString(R.string.upload_in_progress), System.currentTimeMillis()); // set as ongoing mProgressNotification.flags |= Notification.FLAG_ONGOING_EVENT; // set custom view to notification mProgressNotification.contentView = generateProgressNotificationView(0, totalFileLength); //empty intent for the notification final Intent progressIntent = new Intent(); final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, progressIntent, 0); mProgressNotification.contentIntent = contentIntent; // add notification to manager mNotificationManager.notify(NOTIFICATION_ID, mProgressNotification); final InputStream inputStream = getContentResolver().openInputStream(uri); final String boundaryString = "Z." + Long.toHexString(System.currentTimeMillis()) + Long.toHexString((new Random()).nextLong()); final String boundary = "--" + boundaryString; final HttpURLConnection conn = (HttpURLConnection) (new URL("http://imgur.com/api/upload.json")) .openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-type", "multipart/form-data; boundary=\"" + boundaryString + "\""); conn.setUseCaches(false); conn.setDoInput(true); conn.setDoOutput(true); conn.setChunkedStreamingMode(CHUNK_SIZE); final OutputStream hrout = conn.getOutputStream(); final PrintStream hout = new PrintStream(hrout); hout.println(boundary); hout.println("Content-Disposition: form-data; name=\"key\""); hout.println("Content-Type: text/plain"); hout.println(); hout.println(API_KEY); hout.println(boundary); hout.println("Content-Disposition: form-data; name=\"image\""); hout.println("Content-Transfer-Encoding: base64"); hout.println(); hout.flush(); { final Base64OutputStream bhout = new Base64OutputStream(hrout); final byte[] pictureData = new byte[READ_BUFFER_SIZE_BYTES]; int read = 0; int totalRead = 0; long lastLogTime = 0; while (read >= 0) { read = inputStream.read(pictureData); if (read > 0) { bhout.write(pictureData, 0, read); totalRead += read; if (lastLogTime < (System.currentTimeMillis() - PROGRESS_UPDATE_INTERVAL_MS)) { lastLogTime = System.currentTimeMillis(); Log.d(this.getClass().getName(), "Uploaded " + totalRead + " of " + totalFileLength + " bytes (" + (100 * totalRead) / totalFileLength + "%)"); //make a final version of the total read to make the handler happy final int totalReadFinal = totalRead; mHandler.post(new Runnable() { public void run() { mProgressNotification.contentView = generateProgressNotificationView( totalReadFinal, totalFileLength); mNotificationManager.notify(NOTIFICATION_ID, mProgressNotification); } }); } bhout.flush(); hrout.flush(); } } Log.d(this.getClass().getName(), "Finishing upload..."); // This close is absolutely necessary, this tells the // Base64OutputStream to finish writing the last of the data // (and including the padding). Without this line, it will miss // the last 4 chars in the output, missing up to 3 bytes in the // final output. bhout.close(); Log.d(this.getClass().getName(), "Upload complete..."); mProgressNotification.contentView.setProgressBar(R.id.UploadProgress, totalFileLength, totalRead, false); } hout.println(boundary); hout.flush(); hrout.close(); inputStream.close(); Log.d(this.getClass().getName(), "streams closed, " + "now waiting for response from server"); final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); final StringBuilder rData = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { rData.append(line).append('\n'); } return rData.toString(); } catch (final IOException e) { Log.e(this.getClass().getName(), "Upload failed", e); } return null; }
From source file:pt.aptoide.backupapps.data.webservices.ManagerUploads.java
public EnumServerUploadApkStatus uploadApk(ViewApk viewApk, boolean justMd5) { EnumServerUploadApkStatus status = EnumServerUploadApkStatus.SUCCESS; String apkPath = viewApk.getPath(); String token = serviceData.getManagerPreferences().getToken(); String body = formPart("uploadType", "aptbackup"); if (viewApk.getCategory() != null) { body += formPart("category", viewApk.getCategory()); }// w w w . j a v a 2s . c om if (viewApk.getDescription() != null) { body += formPart("description", viewApk.getDescription()); } if (viewApk.getPhone() != null) { body += formPart("apk_phone", viewApk.getPhone()); } if (viewApk.getEmail() != null) { body += formPart("apk_email", viewApk.getEmail()); } if (viewApk.getWebURL() != null) { body += formPart("apk_website", viewApk.getWebURL()); } if (justMd5) { ViewCache apk = serviceData.getManagerCache().getNewViewCache(viewApk.getPath()); serviceData.getManagerCache().calculateMd5Hash(apk); Log.d("Aptoide-ManagerUploads", "UploadApk " + viewApk.getPath() + " - using just md5: " + apk.getMd5sum()); body += formPart("apk_md5sum", apk.getMd5sum()); } else { Log.d("Aptoide-ManagerUploads", "UploadApk " + viewApk.getPath()); } body += formPart("token", token) + formPart("repo", viewApk.getRepository()) + formPart("apkname", viewApk.getName()) + formPart("rating", viewApk.getRating()) + formPart("mode", "xml"); if (!justMd5) { body += formBinaryPartNoTail("apk", "application/vnd.android.package-archive"); } DataOutputStream outputStream = null; try { URL url = new URL(Constants.URI_UPLOAD_WS); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // This fixes #515 : Out of memory bug connection.setChunkedStreamingMode(CHUNK_SIZE); connection.setConnectTimeout(120000); connection.setReadTimeout(120000); // Allow Inputs & Outputs connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); // Enable POST method connection.setRequestMethod("POST"); connection.setInstanceFollowRedirects(true); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); connection.setRequestProperty("User-Agent", getUserAgentString()); outputStream = new DataOutputStream(connection.getOutputStream()); outputStream.writeBytes(body); if (!justMd5) { FileInputStream apk = new FileInputStream(apkPath); byte data[] = new byte[CHUNK_SIZE]; long lenTotal = 0; int read; long uploadSize = viewApk.getSize(); int progressPercentage = 0; while ((read = apk.read(data, 0, CHUNK_SIZE)) != -1) { outputStream.write(data, 0, read); lenTotal += read; int newProgressPercentage = (int) (lenTotal * 100 / uploadSize); Log.d("OutputApk", "sent: " + read + "bytes, Total: " + lenTotal + " completion: " + newProgressPercentage + "% app: " + viewApk.getName()); if (newProgressPercentage > (progressPercentage + 10)) { progressPercentage = newProgressPercentage; serviceData.uploadingProgressUpdate(viewApk.getAppHashid(), progressPercentage); } } } outputStream.writeBytes(LINE_END + TWO_HYPHENS + boundary + TWO_HYPHENS + LINE_END); outputStream.flush(); outputStream.close(); status = serviceData.getManagerXml().dom.parseApkUploadXml(connection); } catch (Exception e) { status = EnumServerUploadApkStatus.CONNECTION_ERROR; e.printStackTrace(); return status; } // catch (MalformedURLException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } catch (ProtocolException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } catch (FileNotFoundException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } catch (IOException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } catch (JSONException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } catch (UnsuccessfullSubmitException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } return status; }
From source file:com.twotoasters.android.hoot.HootTransportHttpUrlConnection.java
private void setRequestMethod(HootRequest request, HttpURLConnection connection) throws ProtocolException { switch (request.getOperation()) { case DELETE://from www . java 2s . c o m connection.setRequestMethod("DELETE"); break; case POST: connection.setRequestMethod("POST"); connection.setDoOutput(true); break; case PUT: connection.setRequestMethod("PUT"); connection.setDoOutput(true); break; case HEAD: connection.setRequestMethod("HEAD"); break; default: connection.setRequestMethod("GET"); break; } if (mStreamingMode == StreamingMode.CHUNKED) { connection.setChunkedStreamingMode(0); } if (request.getOperation() == HootRequest.Operation.PATCH) { request.getHeaders().put("X-HTTP-Method-Override", "PATCH"); } // TODO handle other OP types }
From source file:edu.ncsu.asbransc.mouflon.recorder.UploadFile.java
protected boolean uploadFile(File fileToUpload) { String lineEnding = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; boolean success = true; HttpURLConnection connection = null; try {/*from www. ja v a 2s .c o m*/ URL dest = new URL("http://mouflon.csc.ncsu.edu/cgi-bin/upload.cgi"); connection = (HttpURLConnection) dest.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); connection.setRequestMethod("POST"); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); connection.setChunkedStreamingMode(0); DataOutputStream out = new DataOutputStream(connection.getOutputStream()); out.writeBytes(twoHyphens + boundary + lineEnding); //Log.i("uploadFile", fileToUpload.getName()); out.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"" + fileToUpload.getName() + "\"" + lineEnding); out.writeBytes("Content-Type: application/octet-stream" + lineEnding); out.writeBytes("Content-Transfer-Encoding: base64" + lineEnding); out.writeBytes(lineEnding); encodeFileBase64(fileToUpload, out); //TODO this works fine for small files but for some file size between 256k and 2M it begins failing. out.writeBytes(lineEnding); out.writeBytes(twoHyphens + boundary + twoHyphens + lineEnding); //Log.i("UploadTest", "File uploaded"); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); success = false; } try { BufferedInputStream in = new BufferedInputStream(connection.getInputStream()); byte[] resp = new byte[80]; int read = 0; if ((read = in.read(resp)) > 0) { String responseString = new String(resp, 0, read); //Log.i("uploadFile", responseString); if (!responseString.equals(fileToUpload.getName())) { //Log.e("Upload", "File upload failed"); } } //else //Log.e("Upload", "No response received from server"); } catch (Exception e) { e.printStackTrace(); success = false; } return success; }
From source file:com.trk.aboutme.facebook.Request.java
static HttpURLConnection createConnection(URL url) throws IOException { HttpURLConnection connection; connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty(USER_AGENT_HEADER, getUserAgent()); connection.setRequestProperty(CONTENT_TYPE_HEADER, getMimeContentType()); connection.setChunkedStreamingMode(0); return connection; }