java⽂件流的处理⽂件打包成zip 1、下载⽂件到本地 public void download(HttpServletResponse response){
颜面除皱String filePath ="";//⽂件路径
String fileName ="";//⽂件名称
// 读到流中
InputStream inStream = new FileInputStream(filePath);
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
}
2、java后端下载
⽅式⼀:
new URL(fileUrl + BhFileserver()).openStream()
⽅法⼆:
public Boolean addFile(String url, String id, String fileName) {
RequestCallback requestCallBack = new RequestCallback() {
@Override
public void doWithRequest(ClientHttpRequest request) throws IOException {
}
};
ResponseExtractor<Boolean> responseExtractor = new ResponseExtractor<Boolean>() {
@Override
public Boolean extractData(ClientHttpResponse response) throws IOException {
if (StatusCode() == HttpStatus.OK) {
//得到⽂件流
InputStream input = Body();
return true;
}
return false;
}
};
ute(url, HttpMethod.GET, requestCallBack, responseExtractor, id);
}
3、⽂件打包成zip
public void zipFilesAll() throws Exception {
String zipPath = "";//zip包路径
String zipFileName = "";//zip包名称
File zipFile = new File(zipFileName .toString());
// 创建 FileOutputStream 对象 FileOutputStream fileOutputStream = null;
// 创建 ZipOutputStream
ZipOutputStream zipOutputStream = null;
try {
//创建⽂件夹
zipFile = new File(zipPath );
FileUtils.forceMkdir(zipFile);
//创建⽂件单循环赛
金融时报中文zipFile = new File(zipFileName .toString());
if (!ists()) {
中国日报中文版
}
// 实例化 FileOutputStream 对象
fileOutputStream = new String());
// 实例化 ZipOutputStream 对象
zipOutputStream = new ZipOutputStream(fileOutputStream);
// 创建 ZipEntry 对象
ZipEntry zipEntry = null;
《国家新型城镇化
规划(2014-2020年)》
for (CL cl: ClList) {// 实例化 ZipEntry 对象,源⽂件数组中的当前⽂件
zipEntry = new Clmc() + ".zip");
zipOutputStream.putNextEntry(zipEntry);
} catch (Exception e) {
}finally{
//记得删除⽂件
}
}