KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > jtrac > util > AttachmentUtils


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package info.jtrac.util;
18
19 import java.io.BufferedInputStream JavaDoc;
20 import java.io.BufferedOutputStream JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.net.URLDecoder JavaDoc;
26 import java.net.URLEncoder JavaDoc;
27
28 import javax.servlet.ServletContext JavaDoc;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31
32 /**
33  * Utils that deal with Attachments, upload / download and
34  * file name String manipulation
35  */

36 public class AttachmentUtils {
37     
38     public static String JavaDoc cleanFileName(String JavaDoc path) {
39         // the client browser could be on Unix or Windows, we don't know
40
int index = path.lastIndexOf('/');
41         if (index == -1) {
42             index = path.lastIndexOf('\\');
43         }
44         return (index != -1 ? path.substring(index + 1) : path);
45     }
46     
47     public static void download(ServletContext JavaDoc servletContext, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
48         
49         String JavaDoc fileName = URLDecoder.decode(cleanFileName(request.getRequestURI()), "UTF-8");
50         String JavaDoc filePrefix = request.getParameter("filePrefix");
51         File JavaDoc file = new File JavaDoc(System.getProperty("jtrac.home") + "/attachments/" + filePrefix + "_" + fileName);
52         if (file.canRead()) {
53             InputStream JavaDoc in = null;
54             OutputStream JavaDoc out = null;
55             try {
56                 in = new FileInputStream JavaDoc(file);
57                 if (in != null) {
58                     out = new BufferedOutputStream JavaDoc(response.getOutputStream());
59                     in = new BufferedInputStream JavaDoc(in);
60                     // first try to identify content type for better browser experience
61
String JavaDoc contentType = servletContext.getMimeType(fileName);
62                     if (contentType == null) {
63                         contentType = "application/octet-stream";
64                     }
65                     response.setContentType(contentType);
66                     // Otherwise Firefox will offer only the first word as the default filename
67
String JavaDoc name = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
68                     // disposition as attachment should force a download, instead of using content type
69
// attempt to encode non-ascii characters to UTF-8 and force Firefox to
70
// acknowledge the encoding (with the filename*=... trick)
71
response.setHeader("Content-Disposition", "attachment; filename*=" + name);
72                     int c;
73                     while ((c = in.read()) != -1) {
74                         out.write(c);
75                     }
76                     return;
77                 }
78             } finally {
79                 in.close();
80                 out.close();
81             }
82         }
83         response.sendError(HttpServletResponse.SC_NOT_FOUND);
84     }
85     
86 }
87
Popular Tags