简体   繁体   English

在文件中写入byte []对象并将文件发送到服务器

[英]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). 我试图在Android客户端上的文件中写入byte [](加密的文本),然后将此文件发送到服务器(使用HTTP)。 the file is correctly created in the mobile, but when it is stored at server side, it creates a file with 0 bytes. 该文件是在移动设备中正确创建的,但是当它存储在服务器端时,它将创建一个0字节的文件。

here is the android code to write the file 这是写入文件的android代码

    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 这是读取和存储文件的servlet代码

    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. 由于某些原因,我无法使用Socket。 The android doesn't show any error. android没有显示任何错误。 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 , 问题是,我正在使用URL发送更多数据,即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... 代码以某种方式不允许我首先读取参数,我需要首先读取文件,然后再读取URL中的参数...

Jean and Joop, thank you for the suggestions. 让和乔普,谢谢你的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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