Java生成pdf 不是一个pdf文件或者已损坏

首先,我通过HttpURLConnection去请求一个url。
通过postman解析出来的header是这样的


Connection →keep-alive
X-Powered-By →Undertow/1
Server →WildFly/10
Content-Type →text/plain;charset=UTF-8
Date →Wed, 13 Mar 2019 05:50:43 GMT
Vary →Accept-Encoding
Content-Encoding →gzip
Transfer-Encoding →chunked


body是个字节数组
我后台用inputstream in = con.getInputStream()去接的
我想用流直接把文件输出到指定目录
但是现在有个问题,我文件输出出来了,但一打开就报错 -不是一个pdf文件或者已损坏

阅读 5.9k
1 个回答
private void copyFile(File oldFile, File newFile) throws Exception{
        FileInputStream in = new FileInputStream(oldFile);
        FileOutputStream out = new FileOutputStream(newFile);

        byte[] buffer = new byte[2097152];
        int readByte = 0;
        while((readByte = in.read(buffer)) != -1){
            out.write(buffer, 0, readByte);
        }

        in.close();
        out.close();
    }
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题