首页
登录 | 注册

java响应请求下载文件的http接口

             byte [] pdfbyte = ToolsBox.File2byte(new File(outputFilePath2));
OutputStream stream = null;
try {
stream = response.getOutputStream();
stream.write(pdfbyte);
stream.flush();
stream.close();
} catch(IOException e){
e.printStackTrace();
response.setContentType( “* / *”);
response.setCharacterEncoding( “UTF-8”);
response.setStatus(HttpStatus.NOT_FOUND.value());
try {
response.getWriter()。append(“文件:”+ fileName +“不存在”).append(e.toString());
} catch(IOException e1){
e1.printStackTrace();
}
}最后{
//删除分割后的临时文件
ToolsBox.deleteFile(outputFilePath);
ToolsBox.deleteFile(outputFilePath2);
if(stream!= null){
try {
stream.close();
} catch(IOException e){
e.printStackTrace();
}
}

}




工具类代码:

public static byte [] File2byte(File file){
byte [] buffer = null;
尝试{
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte [] b = new byte [1024];
int n;
while((n = fis.read(b))!= -1){
bos.write(b,0,n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
} catch(FileNotFoundException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}
返回缓冲区;
}



2020 jeepxie.net webmaster#jeepxie.net
10 q. 0.009 s.
京ICP备10005923号