1 package org.tigris.scarab.screens; 2 3 48 49 import java.io.File ; 51 import java.io.BufferedInputStream ; 52 import java.io.FileInputStream ; 53 import java.io.OutputStream ; 54 import javax.servlet.http.HttpServletResponse ; 55 import org.apache.turbine.RunData; 56 import org.apache.turbine.TemplateContext; 57 import org.apache.torque.om.NumberKey; 58 59 import org.tigris.scarab.util.Log; 61 import org.tigris.scarab.om.Attachment; 62 import org.tigris.scarab.om.AttachmentManager; 63 64 70 public class ViewAttachment extends Default 71 { 72 75 public void doBuildTemplate(RunData data, TemplateContext context) 76 throws Exception 77 { 78 super.doBuildTemplate(data, context); 79 80 String attachId = data.getParameters().getString("attachId"); 81 Attachment attachment = AttachmentManager 82 .getInstance(new NumberKey(attachId), false); 83 String contentType = attachment.getMimeType(); 84 HttpServletResponse res = data.getResponse(); 85 res.setContentType(contentType); 86 if (!contentType.startsWith("text")) 90 { 91 res.setHeader("Content-Disposition", 92 "attachment; filename=" + attachment.getFileName()); 93 } 94 95 File f = new File (attachment.getFullPath()); 96 res.setContentLength((int)f.length()); 97 98 BufferedInputStream bis = null; 99 OutputStream os = data.getResponse().getOutputStream(); 100 try 101 { 102 bis = new BufferedInputStream (new FileInputStream (f)); 103 byte[] bytes = new byte[2048]; 104 int s = 0; 105 while ((s = bis.read(bytes)) != -1) 106 { 107 try 108 { 109 os.write(bytes,0,s); 110 } 111 catch (java.io.IOException ioe) 112 { 113 Log.get().debug("File download was aborted: " + 114 attachment.getFullPath()); 115 break; 116 } 117 } 118 } 119 finally 120 { 121 if (bis != null) 122 { 123 bis.close(); 124 } 125 } 126 127 data.setTarget(null); 129 } 130 } 131 132 | Popular Tags |