-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Lars,
Lars Nielsen Lind wrote: | You can do that with a Servlet. Here are some sample code. Replace | content of <> with your own data. This would be a much better class if it: * Did not have extraneous class members. * Used a buffer for reading and writing to the streams. * Did not truncate content-lengths that exceed Integer.MAX_VALUE. * Used the proper MIME type (bug!), or allowed the servlet to ~ override it. * Handled exceptions properly. * Returned proper response codes. * Included documentation (but hey, it's sample code!) :( I point out these problems not to embarrass Lars, but to point out that giving code with these kinds of problems to a likely newbie (apologies, Kimberly, if you are not a newbie... but your question admits a certain level of understanding) does not serve them well. They come to the list hoping to get good advice from good developers and you hand them a class that is rife with problems. My guess is that this kind of thing happens all the time and the recipient is certainly happy to get some working code, but they don't go over it and fix all the problems because it came from someone "smarter" than them. Your code may become an example for how to do certain things. Given that, we should endeavor to provide the highest quality code possible to the readers of this list. We'll be helping them to be better programmers. Allow me to suggest a re-write of this class. package ...; import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class DownloadServlet ~ extends HttpServlet { ~ private static final int BUFFER_SIZE = 4096; ~ private static final String BASE_FILE_PATH = "/path/to/your/files"; ~ protected File findFile(HttpServletRequest request) ~ throws IOException ~ { ~ // This is a reasonable default implementation. ~ // Feel free to change it. ~ File file = new File(BASE_FILE_PATH + request.getPathInfo()); ~ return file; ~ } ~ protected File getMimeType(HttpServletRequest request, File file) ~ { ~ // This is a reasonable default implementation. ~ // Feel free to change it. ~ ServletContext application = request.getServletContext(); ~ return application.getMimeType(file.getName()); ~ } ~ protected void sendFile(File file, HttpServletResponse response) ~ throws IOException ~ { BufferedInputStream in = null; ~ try { int count; ~ byte[] buffer = new byte[BUFFER_SIZE]; ~ in = new BufferedInputStream(new FileInputStream(file)); ~ ServletOutputStream out = response.getOutputStream(); ~ while(-1 != (count = in.read(buffer))) ~ out.write(buffer, 0, count); ~ out.flush(); ~ } ~ finally ~ { ~ if (in != null) ~ { ~ try { in.close(); } ~ catch (IOException ioe) { /* log an error! */ } ~ } ~ } ~ } ~ public void service(HttpServletRequest request, ~ HttpServletResponse response) ~ throws ServletException, IOException ~ { ~ File file = findFile(request); ~ if(null == file || !file.exists()) ~ { ~ response.sendError(HttpServletResponse.SC_NOT_FOUND); ~ } ~ else if(!file.canRead()) ~ { ~ // Feel free to send NOT_FOUND instead, if you don't want to ~ // give up potentially sensitive security information. ~ response.sendError(HttpServletResponse.SC_FORBIDDEN); ~ } ~ else ~ { ~ response.setStatus(HttpServletResponse.SC_OK); ~ response.setContentType(getMimeType(request, file)); ~ response.setHeader("Content-Type", ~ String.valueOf(file.length()); ~ response.setHeader("Content-Disposition", ~ "attachment; filename=" ~ + file.getName()); ~ sendFile(file, response); ~ } ~ } } - -chris -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.8 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEARECAAYFAkfjxWoACgkQ9CaO5/Lv0PBiFACeJuF3Gb91N5pwJlaWDFrVkksb NhYAoKg9CtqkwNqmO2l0iA8+pPBzPM1d =1LA6 -----END PGP SIGNATURE----- --------------------------------------------------------------------- To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]