Thanks for that Chris - very much appreciated, I did manage to get it to compile but when I click on the link in my jsp page everything appears except the image - and also no errors - when I view source it includes the img line for the plot as so: <IMG SRC="/servlet/OpenFile?plot=/home/kimberly/Desktop/test/52-plot.jpg" align="left" ALT="plot">
My jsp includes it as this: <IMG SRC="/servlet/OpenFile?plot=<%=plot%>" align="left" ALT="plot"> Then my serlvet calls the getParameter function to get the absolute path to the jpeg I want to display in the jsp page. I might have messed it up when getting it to compile - I was having problems with the getMimeType method with messages like this: /usr/local/jdk1.5.0_14/bin/javac -d ../../classes/ OpenFile.java OpenFile.java:35: cannot find symbol symbol : method getServletContext() location: interface javax.servlet.http.HttpServletRequest ServletContext application = request.getServletContext(); ^ OpenFile.java:38: incompatible types found : java.lang.String required: java.io.File return application.getMimeType(file.getName()); ^ OpenFile.java:92: setContentType(java.lang.String) in javax.servlet.ServletResponse cannot be applied to (java.io.File) response.setContentType(getMimeType(request, file)); ^ 3 errors I got rid of the first error by changing and adding these linesin the getMimeType method: ServletConfig config = getServletConfig(); ServletContext application = config.getServletContext(); But was still getting the other errors until I commented out the getMimeType method and placed the above 2 lines in the else before calling getMimeType - like this: else { ServletConfig config = getServletConfig(); ServletContext application = config.getServletContext(); response.setStatus(HttpServletResponse.SC_OK); response.setContentType(application.getMimeType(file.getName())); response.setHeader("Content-Type",String.valueOf(file.length())); response.setHeader("Content-Disposition","attachment; filename="+ file.getName()); sendFile(file, response); } So now I'm not sure if it's a problem with the way I'm calling it or if I've messed it up with the changes I've made to it. I've checked the logs and nothing - no errors there either. Thanks for all the assistance with this. Kimberly On Sat, Mar 22, 2008 at 12:25 AM, Christopher Schultz <[EMAIL PROTECTED]> wrote: > -----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] > > -- Kimberly Begley --------------------------------------------------------------------- To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]