File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes file download Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Reply locked New topic
Author

file download

biswajit goswami
Ranch Hand

Joined: Dec 05, 2005
Posts: 35
functionality retrieves file from dbase using hibernate and dialog box appears promting user to open save or cancel...........now the problem:
save it in the system and open the file .....its fine,all the contents of the file are displayed
BUT open the file straight from the dialog box then the appropriate application is triggered but the file is empty...eg if its a txt file then notepad opens it but there is nothing displayed.Here's the code in the action class:
public ActionForward downloadById(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
String fileId=request.getParameter(FILE_ID);
ServiceFactory factory=ServiceFactory.getServiceFactory();
FileBusinessService fileService=(FileBusinessService)factory.getService(FileBusinessService.class);
File file=fileService.retrieveFile(fileId);
String fileName=file.getName();
byte[] fileArray=file.getContent();
String fileType = fileName.substring(fileName.indexOf(".")+1,fileName.length());
setContentTypeHelper(response,fileType,fileName);//see down

ServletOutputStream outStream = response.getOutputStream();
outStream.write(fileArray);
outStream.flush();
outStream.close();

return mapping.findForward("find");

}

private void setContentTypeHelper(HttpServletResponse response,String fileType,String fileName){
if (fileType.trim().equalsIgnoreCase("txt")){
response.setContentType( "text/plain" );
}else if (fileType.trim().equalsIgnoreCase("doc")){
response.setContentType( "application/msword" );
}else if (fileType.trim().equalsIgnoreCase("xls")){
response.setContentType( "application/vnd.ms-excel" );
}else if (fileType.trim().equalsIgnoreCase("pdf")){
response.setContentType( "application/pdf" );
}else if (fileType.trim().equalsIgnoreCase("ppt")){
response.setContentType( "application/ppt" );
}else{
response.setContentType( "application/octet-stream" );
}
response.setHeader("Content-Disposition","attachment; filename=\""+fileName+"\"");
response.setHeader("cache-control", "no-cache");

}
ANY IDEA??
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You've already posted this question in this and another forum.
Please use this one to continue the conversation and stop posting the same question multiple times:
http://www.coderanch.com/t/362577/Servlets/java/File-download-defective-open

A lot of people will refuse to help someone who continues to do this.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: file download
 
Similar Threads
Upload Download File
file download prob......defective open
file download from JSP
blob.getBytes(1, (int) bl.length()); gives Nullpointer Exception
File download problem: defective open