Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Spring
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
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
Forum:
Spring
downloding file prolem(dialog box not coming)
rajareddya reddy
Ranch Hand
Posts: 61
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I am trying to download file using spring controller.
please help any thing wrong with the below code.
String ext = new String(); String msgId = request.getParameter("id"); System.out.println("msgId"+msgId); String fileType = request.getParameter("ftype"); System.out.println("fileType"+fileType); String fileName = transHistoryService.getFileName(msgId, fileType); System.out.println("File path:::" + fileName); logger.info("MessageDetails | fileName::" + fileName); String[] fName = fileName.split("/"); System.out.println("fName"+fName); String fileNameTemp = fName[fName.length - 1]; logger.info("fileNameTemp::" + fileNameTemp); System.out.println("fileNameTemp::" + fileNameTemp); int index = fileNameTemp.lastIndexOf("."); System.out.println("index"+index); if (index != -1) ext = fileNameTemp.substring(index + 1, fileNameTemp.length()); else ext = ""; logger.info("Extension::" + ext); System.out.println("Extension::" + ext); File file = new File(fileName); String Name = file.getName(); logger.info("FileName size" + fileName.length()); logger.info("File Exists: " + file.exists()); System.out.println("Name::" + Name); System.out.println("File Exists::" + file.exists()); System.out.println("FileName size" + fileName.length()); if (file.exists()) { response.setContentType("application/download"); // response.setHeader("Content-Disposition", // "attachment; filename=" + fileNameTemp); response.setHeader("Content-Disposition","attachment; filename=\"" + fileNameTemp + "\";"); // response.setContentType("application/x-download"); File sourceFile = new File(fileName); logger.info("[Public] sourceFile:" + sourceFile); System.out.println("Source File:::" + sourceFile); int size = 0; FileInputStream fis = new FileInputStream(sourceFile); byte[] fileContent = new byte[(int) sourceFile.length()]; logger.info("[Public] fileContent:" + fileContent); System.out.println("File Content::::" + fileContent); fis.read(fileContent); //response.setContentType("application/download"); OutputStream out33 = response.getOutputStream(); InputStream stream = new FileInputStream(file); byte[] bytes = new byte[stream.available()]; stream.read(bytes); // response.setHeader("Content-Disposition", // "attachment; filename=" + fileNameTemp); response.setContentLength(bytes.length); out33.write(fileContent); out33.flush(); stream.close(); out33.close(); logger.info("downloaded sucessfully"); System.out.println("File Downloaded Successfully"); } else { logger.info("file is not present"); }
rajareddya reddy
Ranch Hand
Posts: 61
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
When I am executing above code instead of download dialog box, file content is pasted in the
JSP
page.what might be the problem
Check your pockets for water buffalo. You might need to use this tiny ad until locate a water buffalo:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Issue with Download using ZipOutputStream
blob.getBytes(1, (int) bl.length()); gives Nullpointer Exception
JSF doubts
A problem with servlet file transfer
file download prob......defective open
More...