?
Solved

file rename while downloading

Posted on 2003-02-25
11
Medium Priority
?
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

An introduction to the wonderful sport of Scam Baiting.  Learn how to help fight scammers by beating them at their own game. This great pass time helps the world, while providing an endless source of entertainment. Enjoy!
What's worse than having your data encrypted by ransomware? Getting attacked by a so-called "wiper," which simply destroys the data and offers you no hope of ever seeing it again.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

762 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