msreekm
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
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
ASKER
yeah please suggest me how to do this using servlet or filter...
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.ex t
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("file name");
if (filename != null) {
String filePath = this.getServletContext().g etRealPath ("/") + "/path/to/file/from/Servle t/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(dataR ead, 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("/fi leNotFound .jsp");
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
HTH,
CJ
/downloadServlet?filename=
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("file
if (filename != null) {
String filePath = this.getServletContext().g
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(
// 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(dataR
// close file
if (fileinputstream != null) {
fileinputstream.close();
}
// write the file to output stream
servletoutputstream.write(
// flush the output stream
servletoutputstream.flush(
}
else {
// output something to indicate file does not exist
response.sendRedirect("/fi
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
HTH,
CJ
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().getRea lPath("/" + filename);
String contentType = getServletContext().getMim eType(path Name);
// for files content type is not known
if (contentType != null)
response.setContentType(co ntentType) ;
else
response.setContentType("a pplication /octet-str eam");
response.setHeader ("Content-Disposition", "attachment; filename=\"" + filename + "\"");
I also tried the following:
String pathName = getServletContext().getRea
String contentType = getServletContext().getMim
// for files content type is not known
if (contentType != null)
response.setContentType(co
else
response.setContentType("a
response.setHeader ("Content-Disposition", "attachment; filename=\"" + filename + "\"");
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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..
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..
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..
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..
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..
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</mim e-type>
</mime-mapping>
BTW, grand your points to CJ, and I think you should have done so a month ago :)
<mime-mapping>
<extension>txt</extension>
<mime-type>text/plain</mim
</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
CJ
BTW, you haven't yet closed a question, some of you question was well answered.