Search in sources :

Example 1 with ZipFolder

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 "";
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) ZipFolder(com.ctrip.platform.dal.daogen.utils.ZipFolder) Date(java.util.Date) Project(com.ctrip.platform.dal.daogen.entity.Project) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

Project (com.ctrip.platform.dal.daogen.entity.Project)1 ZipFolder (com.ctrip.platform.dal.daogen.utils.ZipFolder)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1