Code
1public void mDownload()
2{
3try
4{
5Map params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
6String documentId = (String)params.get("documentId");
7if(main.isNum(documentId)==true)
8{
9Query query = main.session.getNamedQuery("upload.selectFileById");
10query.setParameter("id", documentId);
11List list = query.list();
12String userId,oldName,fileName;
13if(list.size()>0)
14{
15Object obj[] = (Object[])list.get(0);
16userId = String.valueOf(obj[0]);
17oldName = String.valueOf(obj[1]);
18fileName = String.valueOf(obj[2]);
19//
20if(main.getFileType(oldName).equals(".nothing"))
21oldName = oldName + main.getFileType(fileName);
22//get the OS name
23String osName = System.getProperty("os.name");
24String storedir = "";
25if(osName == null)
26osName="";
27if(osName.toLowerCase().indexOf("win") != -1)
28storedir = url + userId + "\\";
29else
30storedir = url2 + userId + "/";
31FacesContext ctx = FacesContext.getCurrentInstance();
32ctx.responseComplete();
33//String contentType = "application/octet-stream;charset=utf-8";
34String contentType = "application/x-download";
35HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();
36response.setContentType(contentType);
37StringBuffer contentDisposition = new StringBuffer();
38contentDisposition.append("attachment;");
39contentDisposition.append("filename=\"");
40contentDisposition.append(oldName);
41contentDisposition.append("\"");
42response.setHeader("Content-Disposition", new String(contentDisposition.toString().getBytes(System.getProperty("file.encoding")),"iso8859_1"));
43ServletOutputStream out = response.getOutputStream();
44byte[] bytes = new byte[0xffff];
45InputStream is = new FileInputStream(new File(storedir + fileName));
46int b = 0;
47while ((b = is.read(bytes, 0, 0xffff)) > 0)
48{
49out.write(bytes, 0, b);
50}
51is.close();
52ctx.responseComplete();
53}
54}
55}catch (Exception ex){ex.printStackTrace();}
56}
1public void mDownload()
2{
3try
4{
5Map params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
6String documentId = (String)params.get("documentId");
7if(main.isNum(documentId)==true)
8{
9Query query = main.session.getNamedQuery("upload.selectFileById");
10query.setParameter("id", documentId);
11List list = query.list();
12String userId,oldName,fileName;
13if(list.size()>0)
14{
15Object obj[] = (Object[])list.get(0);
16userId = String.valueOf(obj[0]);
17oldName = String.valueOf(obj[1]);
18fileName = String.valueOf(obj[2]);
19//
20if(main.getFileType(oldName).equals(".nothing"))
21oldName = oldName + main.getFileType(fileName);
22//get the OS name
23String osName = System.getProperty("os.name");
24String storedir = "";
25if(osName == null)
26osName="";
27if(osName.toLowerCase().indexOf("win") != -1)
28storedir = url + userId + "\\";
29else
30storedir = url2 + userId + "/";
31FacesContext ctx = FacesContext.getCurrentInstance();
32ctx.responseComplete();
33//String contentType = "application/octet-stream;charset=utf-8";
34String contentType = "application/x-download";
35HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();
36response.setContentType(contentType);
37StringBuffer contentDisposition = new StringBuffer();
38contentDisposition.append("attachment;");
39contentDisposition.append("filename=\"");
40contentDisposition.append(oldName);
41contentDisposition.append("\"");
42response.setHeader("Content-Disposition", new String(contentDisposition.toString().getBytes(System.getProperty("file.encoding")),"iso8859_1"));
43ServletOutputStream out = response.getOutputStream();
44byte[] bytes = new byte[0xffff];
45InputStream is = new FileInputStream(new File(storedir + fileName));
46int b = 0;
47while ((b = is.read(bytes, 0, 0xffff)) > 0)
48{
49out.write(bytes, 0, b);
50}
51is.close();
52ctx.responseComplete();
53}
54}
55}catch (Exception ex){ex.printStackTrace();}
56}