1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
| public class DownloadServlet extends HttpServlet {
protected void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
System.out.println("DOget Download");
doPost(request, response);
}
public void setFile(
ServletOutputStream out,
HttpServletResponse response,
String fileName,
String path,
String lstrMimeType)
throws ServletException {
try {
FileInputStream in = null;
String lstrDownloadPath = ListLibrary.getDataPath();
FileOutputStream tempfile =
new FileOutputStream(lstrDownloadPath + "/" + fileName);
response.setContentType("multipart/download");
response.setHeader(
"Content-Disposition",
"attachment;filename=" + fileName);
in = new FileInputStream(path);
int availableLength = in.available();
byte[] totalBytes = new byte[availableLength];
System.out.println("avant le setCaontentLength");
response.setContentLength(availableLength);
in.read(totalBytes);
tempfile.write(totalBytes);
System.out.println("Après le 1er write");
tempfile.flush();
tempfile.close();
in.close();
in = new FileInputStream(lstrDownloadPath + "/" + fileName);
availableLength = in.available();
totalBytes = new byte[availableLength];
response.setContentLength(availableLength);
in.read(totalBytes);
out.write(totalBytes);
out.flush();
out.close();
in.close();
File f = new File(lstrDownloadPath + "/" + fileName);
f.delete();
} catch (FileNotFoundException e) {
throw new HermesException(e.getMessage());
} catch (IOException e) {
throw new HermesException(e.getMessage());
}
}
protected void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
System.out.println("DOPOST Download");
try {
DemandeHelper demH =
(DemandeHelper) request.getSession().getAttribute("demH");
ServletContext context =
this.getServletConfig().getServletContext();
Tools.debugRequestParameters(request);
String ext = request.getParameter("FILE");
String plusieurs = request.getParameter("plusieurs");
ServletOutputStream out = response.getOutputStream();
if (plusieurs == null) {
String fileName =
Tools.formatFileName(
demH.getCcpCasTO().getCcpCasId()
+ "-"
+ demH.getReunionCcpHelper().getCcpTO().getCcpLib()
+ "-"
+ demH.getCcpCasTO().getCcpCasDateDemande());
if (ext.equals("pdf")) {
fileName += ".pdf";
} else {
fileName += ".xml";
}
String lstrMimeType = context.getMimeType(fileName);
String path = ListLibrary.getDataPath().concat(fileName);
setFile(out,response, fileName, path, lstrMimeType);
} else {
System.out.println("Dans le else");
Delegate lobjDelegate =
(Delegate) request.getSession().getAttribute("Delegate");
String[] lstrTab = request.getParameterValues("cases");
String[] lstrTabPath = new String[lstrTab.length];
String[] lstrTabFileName = new String[lstrTab.length];
CcpCasTO lobjCcpCasTO2;
String path = ListLibrary.getDataPath();
String fileName = "";
for (int i = 0; i < lstrTab.length; i++) {
System.out.println("Fichiers : " + lstrTab[i]);
lobjCcpCasTO2 =
lobjDelegate.findCcpCasTo(Integer.parseInt(lstrTab[i]));
demH = lobjDelegate.getDemandeHelper(lobjCcpCasTO2);
fileName =
Tools.formatFileName(
demH.getCcpCasTO().getCcpCasId()
+ "-"
+ demH
.getReunionCcpHelper()
.getCcpTO()
.getCcpLib()
+ "-"
+ demH.getCcpCasTO().getCcpCasDateDemande());
if (ext.equals("pdf")) {
fileName += ".pdf";
} else {
fileName += ".xml";
}
lstrTabFileName[i] = fileName;
lstrTabPath[i] = path + fileName;
System.out.println("Nom Fichier : " + lstrTabFileName[i]);
System.out.println("Path : " + path);
}
System.out.println("Après la boucle for");
String lstrMimeType = context.getMimeType("essais.zip");
Tools.zippe(path + "essais.zip", lstrTabPath, lstrTabFileName);
path += fileName;
System.out.println("Après le zip");
setFile(out,response, "essais.zip", path, lstrMimeType);
System.out.println("Après le setFile");
}
RequestDispatcher lobjReqDisp =
request.getRequestDispatcher("/FrontController?action=445");
lobjReqDisp.forward(request, response);
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("Message : "+ex.getMessage());
throw new HermesException(ex.getMessage());
}
}
} |
Partager