01: package org.claros.intouch.webdisk.services;
02:
03: import java.io.BufferedInputStream;
04: import java.io.ByteArrayOutputStream;
05: import java.io.File;
06: import java.io.FileInputStream;
07: import java.io.IOException;
08: import java.io.OutputStream;
09:
10: import javax.servlet.ServletException;
11: import javax.servlet.http.HttpServletRequest;
12: import javax.servlet.http.HttpServletResponse;
13:
14: import org.claros.intouch.common.services.BaseService;
15: import org.claros.intouch.webdisk.controllers.WebdiskController;
16:
17: public class DownloadService extends BaseService {
18:
19: private static final long serialVersionUID = -3552379179879832940L;
20:
21: /**
22: * The doGet method of the servlet. <br>
23: *
24: * This method is called when a form has its tag value method equals to get.
25: *
26: * @param request the request send by the client to the server
27: * @param response the response send by the server to the client
28: * @throws ServletException if an error occurred
29: * @throws IOException if an error occurred
30: */
31: public void doGet(HttpServletRequest request,
32: HttpServletResponse response) throws ServletException,
33: IOException {
34: OutputStream out = response.getOutputStream();
35:
36: try {
37: response.setHeader("Expires", "-1");
38: response.setHeader("Pragma", "no-cache");
39: response.setHeader("Cache-control", "no-cache");
40:
41: String path = request.getParameter("path");
42: String username = getAuthProfile(request).getUsername();
43:
44: File f = WebdiskController.getUserFile(username, path);
45:
46: response.setHeader("Content-disposition",
47: "attachment; filename=\"" + f.getName() + "\"");
48: response.setContentType("application/octet-stream");
49: ByteArrayOutputStream baos = new ByteArrayOutputStream();
50: BufferedInputStream is = new BufferedInputStream(
51: new FileInputStream(f));
52: int byte_;
53: while ((byte_ = is.read()) != -1) {
54: baos.write(byte_);
55: out.flush();
56: }
57: is.close();
58: baos.close();
59: out.write(baos.toByteArray());
60: } catch (Exception e) {
61: e.printStackTrace();
62: }
63: }
64: }
|