请教一下我这段文件下载的代码是否有问题

白白了白呀白 2019-04-30 10:38:43
我用浏览器测试能弹窗下载,但是用postman测试用send and down 我后台打印流已经写完,但是postman却一直卡住

@GetMapping(value = "/test1")
// @ResponseBody
public void test1(HttpServletResponse response) throws XmlPullParserException, NoSuchAlgorithmException, InvalidKeyException, IOException {
try {
// //本地
// MinioClient minioClient = new MinioClient("http://localhost:9000", "Z3TFF3HKCMFZP5BRN62T",
// "GOIuA5leqkPEZDTi2+t2dpM3kc7XiYUoNWb+0E9r");
//服务器
MinioClient minioClient = new MinioClient("http://localhost:9000", "Z3TFF3HKCMFZP5BRN62T",
"GOIuA5leqkPEZDTi2+t2dpM3kc7XiYUoNWb+0E9r");
// Check whether the object exists using statObject().
// If the object is not found, statObject() throws an exception,
// else it means that the object exists.
// Execution is successful.
minioClient.statObject("asiatrip", "asiaphotos.zip");

// Get input stream to have content of 'my-objectname' from 'my-bucketname'
// 配置文件下载
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream");
// 下载文件能正常显示中文
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("asiaphotos.zip", "UTF-8"));

OutputStream outputStream = response.getOutputStream();
InputStream stream = minioClient.getObject("asiatrip", "asiaphotos.zip");

// Read the input stream and print to the console till EOF.
byte[] buf = new byte[16384];
int bytesRead;
while ((bytesRead = stream.read(buf, 0, buf.length)) >= 0) {
System.out.println(new String(buf, 0, bytesRead));
outputStream.write(buf, 0, bytesRead);
}
response.flushBuffer();

// Close the input stream.
stream.close();
outputStream.close();
} catch (MinioException e) {
System.out.println("Error occurred: " + e);
}
}
...全文
52 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
十八道胡同 2019-04-30
  • 打赏
  • 举报
回复
我们就是这么干的,谷歌内可以弹出,postma没测试过
 //通过文件输入输出流下载word
        //设置向浏览器端传送的文件格式
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        try {
            response.setHeader("Content-Disposition", "attachment;fileName=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
            FileInputStream is = new FileInputStream(outFile);
            ServletOutputStream os = response.getOutputStream();
            byte[] b = new byte[4096];

            int length;
            while ((length = is.read(b)) > 0) {
                os.write(b, 0, length);
            }
            os.close();
            is.close();
            outFile.delete();

        } catch (Exception var7) {
            throw new SysException(var7);
        }

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧