| | |
| | | */ |
| | | public MultipartUtilityV2(String requestURL) |
| | | throws IOException { |
| | | |
| | | // creates a unique boundary based on time stamp |
| | | URL url = new URL(requestURL); |
| | | httpConn = (HttpURLConnection) url.openConnection(); |
| | | httpConn.setUseCaches(false); |
| | | httpConn.setDoOutput(true); // indicates POST method |
| | | httpConn.setDoInput(true); |
| | | |
| | | httpConn.setRequestMethod("POST"); |
| | | httpConn.setRequestProperty("Connection", "Keep-Alive"); |
| | | httpConn.setRequestProperty("Cache-Control", "no-cache"); |
| | | httpConn.setRequestProperty( |
| | | "Content-Type", "multipart/form-data;boundary=" + this.boundary); |
| | | |
| | | request = new DataOutputStream(httpConn.getOutputStream()); |
| | | } |
| | | |
| | | public static void uploadCrashDirectory() { |
| | | try { |
| | | File[] crashFiles = new File("/sdcard/crash/").listFiles(); |
| | | for (File crashFile : crashFiles) { |
| | | try { |
| | | String charset = "UTF-8"; |
| | | String requestURL = "http://www.aiotlink.com:8080/UploadServlet"; |
| | | |
| | | MultipartUtilityV2 multipart = new MultipartUtilityV2(requestURL); |
| | | multipart.addFilePart("file", crashFile, crashFile.getName() + RUtils.applicationID + System.currentTimeMillis() + ".txt"); |
| | | String response = multipart.finish(); // response from server. |
| | | System.out.println("MultipartUtilityV2.main" + response); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | try { |
| | | String charset = "UTF-8"; |
| | | String requestURL = "http://www.aiotlink.com:8080/UploadServlet"; |
| | | |
| | | MultipartUtilityV2 multipart = new MultipartUtilityV2(requestURL); |
| | | multipart.addFilePart("file", new File("d:\\c.gif"), new File("d:\\c.gif").getName() + "Security_simplify" + System.currentTimeMillis() + ".txt"); |
| | | String response = multipart.finish(); // response from server. |
| | | System.out.println("MultipartUtilityV2.main" + response); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | } |
| | | |
| | | public static void writeAndUpload(String message) { |
| | | try { |
| | | File file = new File("/sdcard/crash/Security_simplify" + System.currentTimeMillis() + "txt"); |
| | | FileUtils.write(file, message); |
| | | uploadCrashDirectory(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | * @param uploadFile a File to be uploaded |
| | | * @throws IOException |
| | | */ |
| | | public void addFilePart(String fieldName, File uploadFile) |
| | | public void addFilePart(String fieldName, File uploadFile, String fileName) |
| | | throws IOException { |
| | | String fileName = uploadFile.getName(); |
| | | if (fieldName == null || "".equals(fieldName)) { |
| | | fileName = uploadFile.getName(); |
| | | } |
| | | request.writeBytes(this.twoHyphens + this.boundary + this.crlf); |
| | | request.writeBytes("Content-Disposition: form-data; name=\"" + |
| | | fieldName + "\";filename=\"" + |
| | | fileName + "\"" + this.crlf); |
| | | request.writeBytes(this.crlf); |
| | | // byte[] bytes = Files.readAllBytes(uploadFile.toPath()); |
| | | byte[] bytes = FileUtils.readFileToByteArray(uploadFile); |
| | | // byte[] bytes = Files.readAllBytes(uploadFile.toPath()); |
| | | request.write(bytes); |
| | | } |
| | | |
| | | public void addFilePart(String fieldName, File uploadFile) |
| | | throws IOException { |
| | | String fileName = ""; |
| | | if (fieldName == null || "".equals(fieldName)) { |
| | | fileName = uploadFile.getName(); |
| | | } |
| | | request.writeBytes(this.twoHyphens + this.boundary + this.crlf); |
| | | request.writeBytes("Content-Disposition: form-data; name=\"" + |
| | | fieldName + "\";filename=\"" + |
| | | fileName + "\"" + this.crlf); |
| | | request.writeBytes(this.crlf); |
| | | byte[] bytes = FileUtils.readFileToByteArray(uploadFile); |
| | | // byte[] bytes = Files.readAllBytes(uploadFile.toPath()); |
| | | request.write(bytes); |
| | | } |
| | | |
| | |
| | | */ |
| | | public String finish() throws IOException { |
| | | String response = ""; |
| | | |
| | | request.writeBytes(this.crlf); |
| | | request.writeBytes(this.twoHyphens + this.boundary + |
| | | this.twoHyphens + this.crlf); |
| | | |
| | | request.flush(); |
| | | request.close(); |
| | | |
| | | // checks server's status code first |
| | | int status = httpConn.getResponseCode(); |
| | | if (status == HttpURLConnection.HTTP_OK || status == HttpURLConnection.HTTP_CREATED) { |
| | | // System.out.println("MultipartUtilityV2.finish status="+status); |
| | | if (status == HttpURLConnection.HTTP_OK || 1 == 1) { |
| | | InputStream responseStream = new |
| | | BufferedInputStream(httpConn.getInputStream()); |
| | | |
| | | BufferedReader responseStreamReader = |
| | | new BufferedReader(new InputStreamReader(responseStream)); |
| | | |
| | | String line = ""; |
| | | StringBuilder stringBuilder = new StringBuilder(); |
| | | |
| | | while ((line = responseStreamReader.readLine()) != null) { |
| | | stringBuilder.append(line).append("\n"); |
| | | } |
| | | responseStreamReader.close(); |
| | | |
| | | response = stringBuilder.toString(); |
| | | httpConn.disconnect(); |
| | | } else { |
| | | throw new IOException("Server returned non-OK status: " + status); |
| | | } |
| | | |
| | | return response; |
| | | } |
| | | |
| | | } |