| 1 3 package org.contineo.actions.documan.util; 4 5 import java.io.File ; 6 import java.io.FileInputStream ; 7 import java.io.FileNotFoundException ; 8 import java.io.IOException ; 9 import java.io.InputStream ; 10 import java.io.OutputStream ; 11 12 import javax.servlet.http.HttpServletResponse ; 13 14 import org.contineo.admin.Menu; 15 import org.contineo.admin.UserDoc; 16 import org.contineo.admin.dao.MenuDAO; 17 import org.contineo.admin.dao.UserDocDAO; 18 import org.contineo.core.config.MimeTypeConfigurator; 19 import org.contineo.core.config.SettingConfigurator; 20 import org.contineo.documan.Document; 21 import org.contineo.documan.dao.DocumentDAO; 22 23 28 public class DownloadDocUtil { 29 34 public static void addToRecentFiles(String userName, int menuId) { 35 UserDoc userdoc = new UserDoc(); 36 userdoc.setMenuId(menuId); 37 userdoc.setUserName(userName); 38 UserDocDAO uddao = new UserDocDAO(); 39 uddao.store(userdoc); 40 } 41 42 45 public static String getMimeType(Menu docId) { 46 if (docId == null) 47 return null; 48 String extension = docId.getMenuRef().substring(docId.getMenuRef().lastIndexOf(".") + 1); 49 MimeTypeConfigurator mtc = new MimeTypeConfigurator(); 50 String mimetype = mtc.getMimeApp(extension); 51 if (mimetype == null || mimetype.equals("")) 52 mimetype = "application/octet-stream"; 53 return mimetype; 54 } 55 56 62 public static void downloadDocument(HttpServletResponse response, int docId, String docVerId) 63 throws FileNotFoundException , IOException { 64 MenuDAO mdao = new MenuDAO(); 66 Menu menu = mdao.findByPrimaryKey(docId); 67 DocumentDAO ddao = new DocumentDAO(); 68 Document doc = ddao.findByMenuId(docId); 69 if (menu == null || doc == null) { 70 throw new FileNotFoundException (); 71 } 72 73 String mimetype = DownloadDocUtil.getMimeType(menu); 75 76 SettingConfigurator settings = new SettingConfigurator(); 78 String path = settings.getValue("docdir") + menu.getMenuPath() + "/" + menu.getMenuId(); 79 80 String menuref; 83 if (docVerId == null) { 84 menuref = menu.getMenuRef(); 85 } else { 86 menuref = docVerId; 87 } 88 89 File file = new File (path + "/" + menuref); 91 if (!file.exists()) { 92 throw new FileNotFoundException (); 93 } 94 95 response.setContentType(mimetype); 97 response.setHeader("Content-disposition","attachment;filename=" + doc.getDocName().replaceAll(" ", "_") + "." + doc.getDocType()); 98 InputStream is = new FileInputStream (file); 99 OutputStream os; 100 os = response.getOutputStream(); 101 int letter = 0; 102 while ((letter = is.read()) != -1) { 103 os.write(letter); 104 } 105 os.flush(); 106 os.close(); 107 is.close(); 108 } 109 110 116 public static void downloadDocument(HttpServletResponse response, String docId, String docVerId) 117 throws FileNotFoundException , IOException { 118 downloadDocument(response, Integer.parseInt(docId), docVerId); 119 } 120 } | Popular Tags |