1 18 19 20 package org.apache.struts.actions; 21 22 import java.io.BufferedInputStream ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.OutputStream ; 28 29 import javax.servlet.ServletContext ; 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpServletResponse ; 32 33 import org.apache.struts.action.Action; 34 import org.apache.struts.action.ActionForm; 35 import org.apache.struts.action.ActionForward; 36 import org.apache.struts.action.ActionMapping; 37 38 39 50 public abstract class DownloadAction extends Action { 51 52 57 protected static final int DEFAULT_BUFFER_SIZE = 1024 * 4; 58 59 72 protected abstract StreamInfo getStreamInfo(ActionMapping mapping, 73 ActionForm form, HttpServletRequest request, 74 HttpServletResponse response) 75 throws Exception ; 76 77 84 protected int getBufferSize() { 85 return DEFAULT_BUFFER_SIZE; 86 } 87 88 102 public ActionForward execute(ActionMapping mapping, ActionForm form, 103 HttpServletRequest request, HttpServletResponse response) 104 throws Exception { 105 106 StreamInfo info = getStreamInfo(mapping, form, request, response); 107 String contentType = info.getContentType(); 108 InputStream stream = info.getInputStream(); 109 110 try { 111 response.setContentType(contentType); 112 copy(stream, response.getOutputStream()); 113 } finally { 114 if (stream != null) { 115 stream.close(); 116 } 117 } 118 119 return null; 121 } 122 123 134 public int copy(InputStream input, OutputStream output) 135 throws IOException { 136 byte[] buffer = new byte[getBufferSize()]; 137 int count = 0; 138 int n = 0; 139 while (-1 != (n = input.read(buffer))) { 140 output.write(buffer, 0, n); 141 count += n; 142 } 143 return count; 144 } 145 146 150 public static interface StreamInfo { 151 152 157 public abstract String getContentType(); 158 159 165 public abstract InputStream getInputStream() throws IOException ; 166 } 167 168 172 public static class FileStreamInfo implements StreamInfo { 173 174 177 private String contentType; 178 179 182 private File file; 183 184 191 public FileStreamInfo(String contentType, File file) { 192 this.contentType = contentType; 193 this.file = file; 194 } 195 196 201 public String getContentType() { 202 return this.contentType; 203 } 204 205 211 public InputStream getInputStream() throws IOException { 212 FileInputStream fis = new FileInputStream (file); 213 BufferedInputStream bis = new BufferedInputStream (fis); 214 return bis; 215 } 216 } 217 218 222 public static class ResourceStreamInfo implements StreamInfo { 223 224 227 private String contentType; 228 229 232 private ServletContext context; 233 234 237 private String path; 238 239 247 public ResourceStreamInfo(String contentType, ServletContext context, 248 String path) { 249 this.contentType = contentType; 250 this.context = context; 251 this.path = path; 252 } 253 254 259 public String getContentType() { 260 return this.contentType; 261 } 262 263 269 public InputStream getInputStream() throws IOException { 270 return context.getResourceAsStream(path); 271 } 272 } 273 } 274 | Popular Tags |