Пример #1
0
  public String upload(String filepath) throws IOException {
    try {
      connection = (HttpURLConnection) url.openConnection();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      return "";
    }
    connection.setDoOutput(true);
    connection.setReadTimeout(20000);
    connection.setConnectTimeout(20000);
    try {
      connection.setRequestMethod("POST");
    } catch (ProtocolException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    if (connection == null) {
      return "";
    }
    String filename = filepath;
    String fileSuffix = "";

    int pos = filename.lastIndexOf("/");
    filename = filename.substring(pos + 1);
    pos = filename.lastIndexOf(".");
    fileSuffix = filename.substring(pos + 1);
    File file = new File(filepath);
    FileInputStream fis = null;
    fis = new FileInputStream(file);
    long fileLength = file.length();
    connection.setRequestProperty("Content-Type", getMIMEType(fileSuffix));
    connection.setRequestProperty(
        "Content-Length",
        String.valueOf(
            fileLength + boundary.length() + (twoHyphens.length() + lineEnd.length()) * 2));
    connection.setRequestProperty("Connection", "Keep-Alive");
    connection.setRequestProperty("Content-Disposition", "attachment;filename=" + filename);

    DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
    byte[] inputBuffer = new byte[1024];
    int len = 0;
    long totalSentLength = 0;
    while ((len = fis.read(inputBuffer, 0, 1024)) > 0) {
      outputStream.write(inputBuffer, 0, len);
      totalSentLength += len;
      System.out.println("File uploaded:" + (float) totalSentLength / (float) fileLength);
      if (listener != null) {
        listener.onUpload((float) totalSentLength / (float) fileLength);
      }
    }
    outputStream.writeBytes(lineEnd);
    outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
    outputStream.flush();
    BufferedReader outputBuffer =
        new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String returnString = outputBuffer.readLine();
    return returnString;
  }
Пример #2
0
  public String upload(byte[] data, String filePath) throws IOException {
    try {
      connection = (HttpURLConnection) url.openConnection();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      return "";
    }
    connection.setDoOutput(true);
    connection.setReadTimeout(20000);
    connection.setConnectTimeout(20000);
    try {
      connection.setRequestMethod("POST");
    } catch (ProtocolException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    if (connection == null) {
      return "";
    }

    String fileName = this.getFileName(filePath);
    String fileSuffix = this.getSuffix(filePath);

    connection.setRequestProperty("Content-Type", this.getMIMEType(fileSuffix));
    connection.setRequestProperty(
        "Content-Length",
        String.valueOf(
            data.length + boundary.length() + (twoHyphens.length() + lineEnd.length()) * 2));
    connection.setRequestProperty("Connection", "Keep-Alive");
    connection.setRequestProperty("Content-Disposition", "attachment;filename=" + fileName);

    DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
    int len = 0;
    int totalSentLength = 0;
    while (totalSentLength < data.length) {

      if (data.length - totalSentLength > 1024) {
        len = 1024;
      } else {
        len = data.length - totalSentLength;
      }
      outputStream.write(data, totalSentLength, len);
      totalSentLength += len;
      if (listener != null) {
        listener.onUpload((float) totalSentLength / (float) data.length);
      }
    }
    outputStream.writeBytes(lineEnd);
    outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
    outputStream.flush();
    BufferedReader outputBuffer =
        new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String returnString = outputBuffer.readLine();
    return returnString;
  }