Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

file rename while downloading

Posted on 2003-02-25
11
Medium Priority
?
233 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:msreekm
  • 6
  • 3
  • 2
11 Comments
 
LVL 14

Expert Comment

by:kennethxu
ID: 8019541
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
 
LVL 4

Author Comment

by:msreekm
ID: 8022947
yeah please suggest me how to do this using servlet or filter...
0
 
LVL 4

Author Comment

by:msreekm
ID: 8022970
yeah please suggest me how to do this using servlet or filter...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:cheekycj
ID: 8027130
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
 
LVL 4

Author Comment

by:msreekm
ID: 8179282
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
 
LVL 19

Accepted Solution

by:
cheekycj earned 400 total points
ID: 8181801
what do you mean able to save it but not able to open it?

CJ
0
 
LVL 4

Author Comment

by:msreekm
ID: 8185399
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
 
LVL 4

Author Comment

by:msreekm
ID: 8185638
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
 
LVL 4

Author Comment

by:msreekm
ID: 8185648
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
 
LVL 14

Expert Comment

by:kennethxu
ID: 8187142
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
 
LVL 19

Expert Comment

by:cheekycj
ID: 8211111
Thank you for the "A" and good luck :-)

CJ
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Machine Learning is one of the profound applications of AI and therefore, just like AI, it is surrounded by myths and fears. Check out these facts about ML that demystify the related myths.
Often, the users face difficulty in accessing Outlook 2016 PST files on Windows 10 computer. One of the reasons behind it is the improper functioning of MS Outlook when the user tries to open it. MS Outlook suddenly stops working, or it will not op…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question