简体   繁体   中英

write byte[] object in file and send file to server

i'm trying to write an byte[] (encrypted text) in a file on android client, and then sending this file to the Server (using HTTP). the file is correctly created in the mobile, but when it is stored at server side, it creates a file with 0 bytes.

here is the android code to write the file

    File file = new File(getExternalCacheDir(), "index.txt");
    FileOutputStream fout = new FileOutputStream(file);
    ObjectOutputStream oos = new ObjectOutputStream(fout);
    oos.writeObject(encIndex);
    oos.close();

here is the servlet code to read and store the file

    String fileName = request.getHeader("fileName");
    File saveFile = new File(SAVE_DIR + fileName);
    // opens input stream of the request for reading data
    InputStream inputStream = request.getInputStream();

    // opens an output stream for writing file
    FileOutputStream outputStream = new FileOutputStream(saveFile);

    byte[] buffer = new byte[BUFFER_SIZE];
    int bytesRead;
    System.out.println("Receiving data...");

    while ((bytesRead = inputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, bytesRead);
    }

    System.out.println("Data received.");
    outputStream.close();
    inputStream.close();

    System.out.println("File written to: " + saveFile.getAbsolutePath());

    file.close();

Code for Client to Server file sending

    File uploadFile = new File(filePath, fileName);
    URL url = new URL(UPLOAD_URL);
    HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
    httpConn.setUseCaches(false);
    httpConn.setDoOutput(true);
    httpConn.setRequestMethod("POST");
    httpConn.setRequestProperty("fileName", uploadFile.getName());

    OutputStream outputStream = httpConn.getOutputStream();
    FileInputStream inputStream = new FileInputStream(uploadFile);
    byte[] buffer = new byte[BUFFER_SIZE];
    int bytesRead;
    while ((bytesRead = inputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, bytesRead);
    }
    outputStream.close();
    inputStream.close();

for some reasons, i can not use Socket. The android doesn't show any error. the error that server shows is that, the file is empty/ and i'm operating on the empty file

fout.write(encIndex);
fout.close();

Or

BufferedOutputStream oos = new BufferedOutputStream(fout,
    Math.min(4086, encIndex.length));
oos.write(encIndex);
oos.close();

The problem was that, i was sending the some more data with URL, ie https://ip:8084/project/servelt?reqid=something&data=something ,

the code, somehow, don't allow me to read the parameters first, I needed to read the file first, and then the parameters from the URL...

Jean and Joop, thank you for the suggestions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM