Link to home
Start Free TrialLog in
Avatar of msreekm
msreekmFlag for United States of America

asked on

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
Avatar of kennethxu
kennethxu

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.
Avatar of msreekm

ASKER

yeah please suggest me how to do this using servlet or filter...
Avatar of msreekm

ASKER

yeah please suggest me how to do this using servlet or filter...
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
Avatar of msreekm

ASKER

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 + "\"");


ASKER CERTIFIED SOLUTION
Avatar of cheekycj
cheekycj
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of msreekm

ASKER

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..
Avatar of msreekm

ASKER

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..
Avatar of msreekm

ASKER

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..
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 :)
Thank you for the "A" and good luck :-)

CJ