Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

file download prob......defective open

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
biswajit,

Please do not ask the same question in multiple forums.
Doing so wastes the time of those who would help you.
    Bookmark Topic Watch Topic
  • New Topic