Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File download problem: defective open

 
biswajit goswami
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
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??
[ May 05, 2006: Message edited by: Bear Bibeault ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a Hibernate question. Moving to Servlets etc...
 
Sergio Berna
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have experienced that behaviour you describe sometimes and it usually doesnt have to do with the database or the file.

Do you need a session or a identification so that the file can be downloaded? If so you have to bear in mind that some applications such as word or excel download the file themselves outside the navigator with their own http code and session. If that is the case the the cookie specifying the session is lost and so you are no longer logged from the program, resulting in that behaviour.
 
biswajit goswami
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i would like to elaborate the situation:
the dialog box promts for the three options:
save---saves in system and then u open it from system,then all the file contents displayed
but open from the dialog box then the file is opened but with no content
i am unable to figure out whats wrong.........i am not using any session variables or cookies
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic