
 Code
Code1
 public void mDownload()
public void mDownload()2


 {
{3
 try
try4


 {
{5
 Map params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
Map params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();6
 String documentId = (String)params.get("documentId");
String documentId = (String)params.get("documentId");7
 if(main.isNum(documentId)==true)
if(main.isNum(documentId)==true)8


 {
{9
 Query query = main.session.getNamedQuery("upload.selectFileById");
Query query = main.session.getNamedQuery("upload.selectFileById");10
 query.setParameter("id", documentId);
query.setParameter("id", documentId);11
 List list = query.list();
List list = query.list();12
 String userId,oldName,fileName;
String userId,oldName,fileName;13
 if(list.size()>0)
if(list.size()>0)14


 {
{15
 Object obj[] = (Object[])list.get(0);
Object obj[] = (Object[])list.get(0);16
 userId = String.valueOf(obj[0]);
userId = String.valueOf(obj[0]);17
 oldName = String.valueOf(obj[1]);
oldName = String.valueOf(obj[1]);18
 fileName = String.valueOf(obj[2]);
fileName = String.valueOf(obj[2]);19
 //
//20
 if(main.getFileType(oldName).equals(".nothing"))
if(main.getFileType(oldName).equals(".nothing"))21
 oldName = oldName + main.getFileType(fileName);
oldName = oldName + main.getFileType(fileName);22
 //get the OS name
//get the OS name23
 String osName = System.getProperty("os.name");
String osName = System.getProperty("os.name");24
 String storedir  = "";
String storedir  = "";25
 if(osName == null)
if(osName == null)26
 osName="";
osName="";27
 if(osName.toLowerCase().indexOf("win") != -1)
if(osName.toLowerCase().indexOf("win") != -1)28
 storedir = url + userId + "\\";
storedir = url + userId + "\\";29
 else
else30
 storedir = url2 + userId + "/";
storedir = url2 + userId + "/";31
 FacesContext ctx = FacesContext.getCurrentInstance();
FacesContext ctx = FacesContext.getCurrentInstance();32
 ctx.responseComplete();
ctx.responseComplete();33
 //String contentType = "application/octet-stream;charset=utf-8";
//String contentType = "application/octet-stream;charset=utf-8";34
 String contentType = "application/x-download";
String contentType = "application/x-download";35
 HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();
HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();36
 response.setContentType(contentType);
response.setContentType(contentType);37
 StringBuffer contentDisposition = new StringBuffer();
StringBuffer contentDisposition = new StringBuffer();38
 contentDisposition.append("attachment;");
contentDisposition.append("attachment;");39
 contentDisposition.append("filename=\"");
contentDisposition.append("filename=\"");40
 contentDisposition.append(oldName);
contentDisposition.append(oldName);41
 contentDisposition.append("\"");
contentDisposition.append("\"");42
 response.setHeader("Content-Disposition", new String(contentDisposition.toString().getBytes(System.getProperty("file.encoding")),"iso8859_1"));
response.setHeader("Content-Disposition", new String(contentDisposition.toString().getBytes(System.getProperty("file.encoding")),"iso8859_1"));43
 ServletOutputStream out = response.getOutputStream();
ServletOutputStream out = response.getOutputStream();44
 byte[] bytes = new byte[0xffff];
byte[] bytes = new byte[0xffff];45
 InputStream is = new FileInputStream(new File(storedir + fileName));
InputStream is = new FileInputStream(new File(storedir + fileName));46
 int b = 0;
int b = 0;47
 while ((b = is.read(bytes, 0, 0xffff)) > 0)
while ((b = is.read(bytes, 0, 0xffff)) > 0)48


 {
{49
 out.write(bytes, 0, b);
out.write(bytes, 0, b);50
 }
}51
 is.close();
is.close();52
 ctx.responseComplete();
ctx.responseComplete();53
 }
}               54
 }
}55

 }catch (Exception ex)
}catch (Exception ex) {ex.printStackTrace();}
{ex.printStackTrace();}56
 }
} 
                    
                     
                    
                 
                    
                 
         
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号