0

i done file uploading using :

                 ServletContext servletContext = getServletContext();
         String contextPath = servletContext.getRealPath(File.separator);

         String path = contextPath + "\\uploads\\" + session.getAttribute("seusername");
         System.out.println(path);

         File file=new File(path);
         if(!file.exists())
             file.mkdirs();
         Part filePart = request.getPart("uploadfile");
         //return content type of the file
         String type = filePart.getHeader("content-type");

         //checking content type of file. 
         if (!type.equals("application/x-msdownload")) {

             final String fileName = getFileName(filePart);
             myfilename=fileName;
            try {
                 EncriptFilename= aESCryp.encrypt(fileName);
                System.out.println(EncriptFilename);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


             OutputStream fout = null;
             InputStream filecontent = null;

             try {
                 fout = new FileOutputStream(new File(path + File.separator + fileName));
                 filecontent = filePart.getInputStream();
                 int read = 0;
                 final byte[] bytes = new byte[32 * 1024];

                 while ((read = filecontent.read(bytes)) != -1) {
                     fout.write(bytes, 0, read);
                 }

                 fout.flush();
                 fout.close();
             } catch (Exception er) {
                 System.out.println("error:"+er.getMessage());
             }
         }

I am uploaded image,pdf,doc files ,,,its is fine.. after the file location on my local disc folder. D:\JavaWorkspace.metadata.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\File\uploads\user\java_coffee_cup_logo1600.png

my question is ...how to download this file ,,, i can't download with href link..

1 Answers1

0

Your web app can support file downloads by essentially doing the opposite of what the Servlet in your post is doing.

Create a "Download" Servlet, and configure a mapping to the Servlet in your web.xml (or use annotations to define the mapping). The URL to this servlet could look like: http://machine.com/my-app/download/my-file.jpg

In the Download Servlet, look at the request URL to discover the requested file name (my-file.jpg), and then use a FileInputStream to open and read my-file.jpg. request.getPathInfo() will likely give you the information you need to determine the file the user wants to download. See the javadoc for HttpServletRequest.getPathInfo().

Note that my-file.jpg can exist anywhere you want. Your Servlet can map the file name and path in the request URL to an arbitrary place on your local file system. The file could even exist on another web server. You just need to be able to create an InputStream that accesses the file.

Using this path information about the file, create a FileInputStream to access the file. Then copy the FileInputStream to the ServletResponse's output stream. This SO post and this SO post give examples how to do copy an InputStream to an OutputStream.

You can get the response's output stream like this: response.getOutputStream(). Don't forget to close the InputStream and OutputStream when you are done.

Community
  • 1
  • 1
StvnBrkdll
  • 3,924
  • 1
  • 24
  • 31