use of com.ctrip.platform.dal.daogen.utils.ZipFolder in project dal by ctripcorp.
the class FileResource method download.
@GET
@Path("download")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public String download(@QueryParam("id") String id, @QueryParam("language") String name, @Context HttpServletRequest request, @Context HttpServletResponse response) throws Exception {
File f = null;
if (null != name && !name.isEmpty()) {
f = new File(new File(generatePath, id), name);
} else {
f = new File(generatePath, id);
}
Project proj = SpringBeanGetter.getDaoOfProject().getProjectByID(Integer.valueOf(id));
DateFormat format1 = new SimpleDateFormat("yyyyMMddHHmmss");
String date = format1.format(new Date());
final String zipFileName = proj.getName() + "-" + date + ".zip";
if (f.isFile()) {
zipFile(f, zipFileName);
} else {
new ZipFolder(f.getAbsolutePath()).zipIt(zipFileName);
}
FileInputStream fis = null;
BufferedInputStream buff = null;
OutputStream myout = null;
String path = generatePath + "/" + zipFileName;
File file = new File(path);
try {
if (!file.exists()) {
response.sendError(404, "File not found!");
return "";
} else {
response.setContentType("application/zip;charset=utf-8");
response.setContentLength((int) file.length());
response.setHeader("Content-Disposition", "attachment;filename=" + new String(file.getName().getBytes(Charsets.UTF_8), "UTF-8"));
}
// response.reset();
fis = new FileInputStream(file);
buff = new BufferedInputStream(fis);
byte[] b = new byte[1024];
long k = 0;
myout = response.getOutputStream();
while (k < file.length()) {
int j = buff.read(b, 0, 1024);
k += j;
myout.write(b, 0, j);
}
myout.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
if (buff != null)
buff.close();
if (myout != null)
myout.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return "";
}
Aggregations