file rename while downloading

How do i dynamically rename a file stored on a server  while downloading by setting mime or any other method if available.

Basically I have a set of user uploaded files stored in my web server with a number added as [id_filename.extn] whiloe downloading i want to rename it to [filename.extn] removing the id ,but not using the filesystem renaming.

thanks
sree
LVL 4
msreekmAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kennethxuCommented:
you need a servlet or filter to achieve this. are you ready for that?

BTW, you haven't yet closed a question, some of you question was well answered.
0
msreekmAuthor Commented:
yeah please suggest me how to do this using servlet or filter...
0
msreekmAuthor Commented:
yeah please suggest me how to do this using servlet or filter...
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

cheekycjCommented:
create a servlet that gets the filename passed in the URL:

/downloadServlet?filename=blah123.ext

public class DownloadServlet extends HttpServlet {

  public void init(ServletConfig config) throws ServletException { super.init(config); }

  public void destroy() { super.destroy(); }

  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String filename = request.getParameter("filename");
    if (filename != null) {
      String filePath = this.getServletContext().getRealPath("/") + "/path/to/file/from/Servlet/Context"; // you can replace this with the full path to the directory where the files are if you know it.
      File yourfile = new File(filePath + filename);
      if (yourfile.exists()) {
         // change the code below change the filename to whatever you desire
         String customFilename = filename;
         ServletConfig config = getServletConfig();
         ServletContext application = config.getServletContext();
         response.setContentType ("application/octet-stream");
         response.setHeader ("Content-Disposition", "attachment; filename=\"" + customFilename + "\"");
         response.setContentLength((int) yourfile.length());
         // set output stream to the response's output stream
         ServletOutputStream servletoutputstream = response.getOutputStream();
         // now read file
         byte[] dataRead = new byte[(int)yourfile.length()];
         FileInputStream fileinputstream = new FileInputStream(textFile);
         // read in byte data from file and store in byte array
         fileinputstream.read(dataRead, 0, (int)yourfile.length());
         // close file
         if (fileinputstream != null) {
           fileinputstream.close();
         }
         // write the file to output stream
         servletoutputstream.write(dataRead);
         // flush the output stream
         servletoutputstream.flush();
       }
       else {
          // output something to indicate file does not exist
          response.sendRedirect("/fileNotFound.jsp");
       }
    }
  }

  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.doGet(request, response);
  }

}

HTH,
CJ
0
msreekmAuthor Commented:
Thanks that code works, but I have a problem downloading .txt file using the above code.  Iam able to save the txt file, but unable to open the file (ie version iam using is 5.0)

I also tried the following:

String pathName = getServletContext().getRealPath("/" + filename);
     String contentType = getServletContext().getMimeType(pathName);
    // for files content type is not known
    if (contentType != null)
        response.setContentType(contentType);
     else
    response.setContentType("application/octet-stream");
    response.setHeader ("Content-Disposition", "attachment; filename=\"" + filename + "\"");


0
cheekycjCommented:
what do you mean able to save it but not able to open it?

CJ
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
msreekmAuthor Commented:
Hi
     I mean when i click on the link for the filename uploaded in the browser ,a dialog box with open/save as appear for all types, for text content only Iam not able to open by selecting 'Open' in the dialog box, Iam able to 'Save' to my local machine. It works for typese like excel,doc..
0
msreekmAuthor Commented:
Hi
     I mean when i click on the link for the filename uploaded in the browser ,a dialog box with open/save as appear for all types, for text content only Iam not able to open by selecting 'Open' in the dialog box, Iam able to 'Save' to my local machine. It works for typese like excel,doc..
0
msreekmAuthor Commented:
Hi
     I mean when i click on the link for the filename uploaded in the browser ,a dialog box with open/save as appear for all types, for text content only Iam not able to open by selecting 'Open' in the dialog box, Iam able to 'Save' to my local machine. It works for typese like excel,doc..
0
kennethxuCommented:
add this to your application's \web-inf\web.xml
<mime-mapping>
<extension>txt</extension>
<mime-type>text/plain</mime-type>
</mime-mapping>


BTW, grand your points to CJ, and I think you should have done so a month ago :)
0
cheekycjCommented:
Thank you for the "A" and good luck :-)

CJ
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.