Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 收到的文件比发送的文件大_Java_File_Spring Restcontroller - Fatal编程技术网

Java 收到的文件比发送的文件大

Java 收到的文件比发送的文件大,java,file,spring-restcontroller,Java,File,Spring Restcontroller,这是发送文件的代码: byte[] data = FileUtils.readFileToByteArray(new File("my_file.docx")); System.out.println(data.length); // prints 6408 ResponseEntity<byte[]> responseEntity = makeResponse(data, HttpStatus.OK, DOCX); return responseEntity; priva

这是发送文件的代码:

byte[] data = FileUtils.readFileToByteArray(new File("my_file.docx"));

System.out.println(data.length); // prints 6408

ResponseEntity<byte[]> responseEntity = makeResponse(data, HttpStatus.OK, DOCX);

return responseEntity;


private <T> ResponseEntity<T> makeResponse(T responseParameter, HttpStatus httpStatus,
                                           DocumentFormat documentFormat) {
    HttpHeaders headers = new HttpHeaders();

    String filename;

    switch (documentFormat) {
        case PDF:
            headers.setContentType(MediaType.parseMediaType("application/pdf"));
            filename = "output.pdf";
            break;
        case DOCX:
            headers.setContentType(MediaType.parseMediaType("application/docx"));
            filename = "output.docx";
            break;
        default:
            throw new IllegalArgumentException(documentFormat.name() + "is not supported");
    }

    headers.setContentDispositionFormData(filename, filename);
    return new ResponseEntity<>(responseParameter, headers, httpStatus);
}
byte[]data=FileUtils.readFileToByteArray(新文件(“my_File.docx”);
System.out.println(data.length);//打印6408
ResponseEntity ResponseEntity=makeResponse(数据,HttpStatus.OK,DOCX);
返回响应性;
私有响应生成响应(T响应参数、HttpStatus HttpStatus、,
文档格式(文档格式){
HttpHeaders=新的HttpHeaders();
字符串文件名;
开关(文档格式){
案例PDF:
headers.setContentType(MediaType.parseMediaType(“application/pdf”);
filename=“output.pdf”;
打破
案例文档:
headers.setContentType(MediaType.parseMediaType(“应用程序/docx”));
filename=“output.docx”;
打破
违约:
抛出新的IllegalArgumentException(不支持documentFormat.name()+);
}
headers.setContentDispositionFormData(文件名、文件名);
返回新的ResponseEntity(responseParameter、Header、httpStatus);
}

收到的文件大小为8546字节。发送的文件大小为6408字节。即使编码有点错误,收到的文件也应该是相同大小的,对吗?接收到的文件的内部看起来像两页随机字符,“uesdbbqacagianqvt0yaaaaaaa”不清楚到底是什么在发送文件,但它是以base64的形式传输的-这就是为什么它的大小是原始文件的4/3

我很确定,如果您通过base64解码器运行收到的文件,您将获得原始数据。看看标题,我想你会发现其中提到了base64


这可能会提示您如何在不使用base64的情况下发送文件-可能您正在使用的框架中默认使用base64,并且您可能会覆盖它以仅以二进制形式发送数据。

不清楚实际发送文件的是什么,但是它是以base64的形式传输的,这就是为什么它的大小是原版的4/3

我很确定,如果您通过base64解码器运行收到的文件,您将获得原始数据。看看标题,我想你会发现其中提到了base64

这可能会提示您如何在不使用base64的情况下发送文件—可能是您正在使用的框架中的base64默认值,您可能会覆盖它以二进制形式发送数据