List of usage examples for java.io DataOutputStream writeBytes
public final void writeBytes(String s) throws IOException
From source file:org.xingjitong.ChatFragment.java
private String uploadImage(String filePath, Bitmap file, int compressorQuality, final int imageSize) { String fileName;/* ww w . j a v a 2s . c o m*/ if (filePath != null) { File sourceFile = new File(filePath); fileName = sourceFile.getName(); } else { fileName = getString(R.string.temp_photo_name_with_date).replace("%s", String.valueOf(System.currentTimeMillis())); } String response = null; HttpURLConnection conn = null; try { String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "---------------------------14737809831466499882746641449"; URL url = new URL(uploadServerUri); conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("ENCTYPE", "multipart/form-data"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); conn.setRequestProperty("uploaded_file", fileName); ProgressOutputStream pos = new ProgressOutputStream(conn.getOutputStream()); pos.setListener(new OutputStreamListener() { @Override public void onBytesWrite(int count) { bytesSent += count; progressBar.setProgress(bytesSent * 100 / imageSize); } }); DataOutputStream dos = new DataOutputStream(pos); dos.writeBytes(lineEnd + twoHyphens + boundary + lineEnd); dos.writeBytes( "Content-Disposition: form-data; name=\"userfile\"; filename=\"" + fileName + "\"" + lineEnd); dos.writeBytes("Content-Type: application/octet-stream" + lineEnd); dos.writeBytes(lineEnd); file.compress(CompressFormat.JPEG, compressorQuality, dos); dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); dos.flush(); dos.close(); InputStream is = conn.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int bytesRead; byte[] bytes = new byte[1024]; while ((bytesRead = is.read(bytes)) != -1) { baos.write(bytes, 0, bytesRead); } byte[] bytesReceived = baos.toByteArray(); baos.close(); is.close(); response = new String(bytesReceived); } catch (Exception e) { e.printStackTrace(); } finally { if (conn != null) { conn.disconnect(); } } return response; }
From source file:com.appbase.androidquery.callback.AbstractAjaxCallback.java
private static void writeField(DataOutputStream dos, String name, String value) throws IOException { dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"" + name + "\""); dos.writeBytes(lineEnd);//from ww w.j ava 2 s . c o m dos.writeBytes(lineEnd); byte[] data = value.getBytes("UTF-8"); dos.write(data); dos.writeBytes(lineEnd); }
From source file:br.org.indt.ndg.servlets.ReceiveImage.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { log.debug("RECEIVE IMAGE: executing doPost method."); response.reset();//from w w w . java 2s . co m response.setContentType("image/jpg"); InputStream inputStream = request.getInputStream(); DataOutputStream dataOutputStream = new DataOutputStream(response.getOutputStream()); if (inputStream != null) { log.debug("RECEIVE IMAGE: creating file ImageFromClient.jpg..."); FileOutputStream fileOutputStream = new FileOutputStream("ImageFromClient.jpg"); byte buffer[] = new byte[1024 * 128]; int i = 0; while ((i = inputStream.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, i); } fileOutputStream.close(); inputStream.close(); dataOutputStream.writeBytes(SUCCESS); dataOutputStream.close(); log.debug("RECEIVE IMAGE: SUCCESS"); } else { dataOutputStream.writeBytes(FAILURE); log.debug("RECEIVE IMAGE: FAILURE"); } }
From source file:com.jamsuni.jamsunicodescan.MainActivity.java
public void testTTS(String msg) { String apiURL = "https://openapi.naver.com/v1/voice/tts.bin"; BookInfo bookInfo = new BookInfo(); try {/*from w ww. j a va 2 s. c o m*/ String text = URLEncoder.encode(msg, "UTF-8"); URL url = new URL(apiURL); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("POST"); con.setRequestProperty("X-Naver-Client-Id", clientId); con.setRequestProperty("X-Naver-Client-Secret", clientSecret); // post request String postParams = "speaker=mijin&speed=0&text=" + text; con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(postParams); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); BufferedReader br; if (responseCode == 200) { // ? InputStream is = con.getInputStream(); int read = 0; byte[] bytes = new byte[1024]; // ? ? mp3 ? ? String tempname = Long.valueOf(new Date().getTime()).toString(); File f = new File(Environment.getExternalStorageDirectory() + "/" + tempname + ".mp3"); f.createNewFile(); OutputStream outputStream = new FileOutputStream(f); while ((read = is.read(bytes)) != -1) { outputStream.write(bytes, 0, read); } is.close(); } else { // ? ? br = new BufferedReader(new InputStreamReader(con.getErrorStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = br.readLine()) != null) { response.append(inputLine); } br.close(); System.out.println(response.toString()); } } catch (Exception e) { System.out.println(e); } return; }
From source file:se.leap.bitmaskclient.ProviderAPI.java
/** * Executes an HTTP request expecting a JSON response. * * @param url/* w w w.j a v a 2s . c o m*/ * @param request_method * @param parameters * @return response from authentication server */ private JSONObject sendToServer(String url, String request_method, Map<String, String> parameters) { JSONObject json_response; HttpsURLConnection urlConnection = null; try { InputStream is = null; urlConnection = (HttpsURLConnection) new URL(url).openConnection(); urlConnection.setRequestMethod(request_method); String locale = Locale.getDefault().getLanguage() + Locale.getDefault().getCountry(); urlConnection.setRequestProperty("Accept-Language", locale); urlConnection.setChunkedStreamingMode(0); urlConnection.setSSLSocketFactory(getProviderSSLSocketFactory()); DataOutputStream writer = new DataOutputStream(urlConnection.getOutputStream()); writer.writeBytes(formatHttpParameters(parameters)); writer.close(); is = urlConnection.getInputStream(); String plain_response = new Scanner(is).useDelimiter("\\A").next(); json_response = new JSONObject(plain_response); } catch (ClientProtocolException e) { json_response = getErrorMessage(urlConnection); e.printStackTrace(); } catch (IOException e) { json_response = getErrorMessage(urlConnection); e.printStackTrace(); } catch (JSONException e) { json_response = getErrorMessage(urlConnection); e.printStackTrace(); } catch (NoSuchAlgorithmException e) { json_response = getErrorMessage(urlConnection); e.printStackTrace(); } catch (KeyManagementException e) { json_response = getErrorMessage(urlConnection); e.printStackTrace(); } catch (KeyStoreException e) { json_response = getErrorMessage(urlConnection); e.printStackTrace(); } catch (CertificateException e) { json_response = getErrorMessage(urlConnection); e.printStackTrace(); } return json_response; }
From source file:com.ichi2.anki.SyncClient.java
public static void fullSyncFromLocal(String password, String username, String deckName, String deckPath) { URL url;/*from w w w .j av a2s.c om*/ try { Log.i(AnkiDroidApp.TAG, "Fullup"); url = new URL(AnkiDroidProxy.SYNC_URL + "fullup"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "close"); conn.setRequestProperty("Charset", "UTF-8"); // conn.setRequestProperty("Content-Length", "8494662"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + MIME_BOUNDARY); conn.setRequestProperty("Host", AnkiDroidProxy.SYNC_HOST); DataOutputStream ds = new DataOutputStream(conn.getOutputStream()); Log.i(AnkiDroidApp.TAG, "Pass"); ds.writeBytes(TWO_HYPHENS + MIME_BOUNDARY + END); ds.writeBytes("Content-Disposition: form-data; name=\"p\"" + END + END + password + END); Log.i(AnkiDroidApp.TAG, "User"); ds.writeBytes(TWO_HYPHENS + MIME_BOUNDARY + END); ds.writeBytes("Content-Disposition: form-data; name=\"u\"" + END + END + username + END); Log.i(AnkiDroidApp.TAG, "DeckName"); ds.writeBytes(TWO_HYPHENS + MIME_BOUNDARY + END); ds.writeBytes("Content-Disposition: form-data; name=\"d\"" + END + END + deckName + END); Log.i(AnkiDroidApp.TAG, "Deck"); ds.writeBytes(TWO_HYPHENS + MIME_BOUNDARY + END); ds.writeBytes("Content-Disposition: form-data; name=\"deck\";filename=\"deck\"" + END); ds.writeBytes("Content-Type: application/octet-stream" + END); ds.writeBytes(END); FileInputStream fStream = new FileInputStream(deckPath); byte[] buffer = new byte[Utils.CHUNK_SIZE]; int length = -1; Deflater deflater = new Deflater(Deflater.BEST_SPEED); DeflaterOutputStream dos = new DeflaterOutputStream(ds, deflater); Log.i(AnkiDroidApp.TAG, "Writing buffer..."); while ((length = fStream.read(buffer)) != -1) { dos.write(buffer, 0, length); Log.i(AnkiDroidApp.TAG, "Length = " + length); } dos.finish(); fStream.close(); ds.writeBytes(END); ds.writeBytes(TWO_HYPHENS + MIME_BOUNDARY + TWO_HYPHENS + END); Log.i(AnkiDroidApp.TAG, "Closing streams..."); ds.flush(); ds.close(); // Ensure we got the HTTP 200 response code int responseCode = conn.getResponseCode(); if (responseCode != 200) { Log.i(AnkiDroidApp.TAG, "Response code = " + responseCode); // throw new Exception(String.format("Received the response code %d from the URL %s", responseCode, // url)); } else { Log.i(AnkiDroidApp.TAG, "Response code = 200"); } // Read the response InputStream is = conn.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] bytes = new byte[1024]; int bytesRead; while ((bytesRead = is.read(bytes)) != -1) { baos.write(bytes, 0, bytesRead); } byte[] bytesReceived = baos.toByteArray(); baos.close(); is.close(); String response = new String(bytesReceived); Log.i(AnkiDroidApp.TAG, "Finished!"); } catch (MalformedURLException e) { Log.i(AnkiDroidApp.TAG, "MalformedURLException = " + e.getMessage()); } catch (IOException e) { Log.i(AnkiDroidApp.TAG, "IOException = " + e.getMessage()); } }
From source file:com.smartbear.collaborator.issue.IssueRest.java
/** * Uploads zip file of raw files to Collaborator server * //from ww w . ja va 2 s. co m * @param targetZipFile * @throws Exception */ private void uploadRawFilesToCollab(java.io.File targetZipFile) throws Exception { HttpURLConnection httpUrlConnection = null; try { String crlf = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; URL url = new URL(configModel.getUrl() + URI_COLAB_UPLOAD); httpUrlConnection = (HttpURLConnection) url.openConnection(); httpUrlConnection.setUseCaches(false); httpUrlConnection.setDoOutput(true); httpUrlConnection.setRequestMethod("POST"); httpUrlConnection.setRequestProperty("Connection", "Keep-Alive"); httpUrlConnection.setRequestProperty("Cache-Control", "no-cache"); httpUrlConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); String loginCookie = "CodeCollaboratorLogin=" + configModel.getLogin(); String ticketCookie = "CodeCollaboratorTicketId=" + configModel.getAuthTicket(); httpUrlConnection.setRequestProperty("Cookie", loginCookie + "," + ticketCookie); DataOutputStream request = new DataOutputStream(httpUrlConnection.getOutputStream()); request.writeBytes(twoHyphens + boundary + crlf); request.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\"" + targetZipFile.getName() + "\"" + crlf); request.writeBytes("Content-Type: application/x-zip-compressed" + crlf); request.writeBytes(crlf); InputStream fileInStream = new FileInputStream(targetZipFile); request.write(IOUtils.toByteArray(fileInStream)); request.writeBytes(crlf); request.writeBytes(twoHyphens + boundary + twoHyphens + crlf); request.flush(); request.close(); if (httpUrlConnection.getResponseCode() != HttpURLConnection.HTTP_OK) { throw new Exception(); } } catch (Exception e) { LOGGER.error(e); throw new Exception( "Can't upload raw versions to Collaborator Server. Check plugin collaborator configuration (url, login, password).", e); } finally { if (httpUrlConnection != null) { httpUrlConnection.disconnect(); } } }
From source file:com.appbase.androidquery.callback.AbstractAjaxCallback.java
private static void writeData(DataOutputStream dos, String name, String filename, InputStream is) throws IOException { dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"" + name + "\";" + " filename=\"" + filename + "\"" + lineEnd);/*from w w w .j a va2 s . c o m*/ //added to specify type dos.writeBytes("Content-Type: application/octet-stream"); dos.writeBytes(lineEnd); dos.writeBytes("Content-Transfer-Encoding: binary"); dos.writeBytes(lineEnd); dos.writeBytes(lineEnd); AQUtility.copy(is, dos); dos.writeBytes(lineEnd); }
From source file:org.apache.hadoop.hdfs.TestDFSShell.java
static Path writeFile(FileSystem fs, Path f) throws IOException { DataOutputStream out = fs.create(f); out.writeBytes("dhruba: " + f); out.close();//from ww w .java 2 s. c o m assertTrue(fs.exists(f)); return f; }
From source file:com.gotraveling.external.androidquery.callback.AbstractAjaxCallback.java
private static void writeData(DataOutputStream dos, String name, String filename, InputStream is, Progress p) throws IOException { dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"" + name + "\";" + " filename=\"" + filename + "\"" + lineEnd);/*from w w w . j a va 2 s . c o m*/ //added to specify type dos.writeBytes("Content-Type: application/octet-stream"); dos.writeBytes(lineEnd); dos.writeBytes("Content-Transfer-Encoding: binary"); dos.writeBytes(lineEnd); dos.writeBytes(lineEnd); // AQUtility.copy(is, dos); AQUtility.copy(is, dos, is.available(), p); dos.writeBytes(lineEnd); }