aspose file tools
The moose likes Spring and the fly likes File Download via Spring does not work in Firefox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Groovy Fundamentals video training course this week in the Groovy forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "File Download via Spring does not work in Firefox" Watch "File Download via Spring does not work in Firefox" New topic

File Download via Spring does not work in Firefox

ShruthiJ Rao

Joined: Dec 29, 2011
Posts: 1

I have written a program to upload and download files (.doc, .xls and .txt) from DB (mySQL). It is written using Spring MVC. The upload and download is working correctly in IE however in Firefox it is not working as excepted.

To download the file a link is provided in the JSP on click of which the "File Download" dialog box will pop-up. In IE this dialog box pops up and allows to open and/or save the file in the correct format (i.e. .doc, .xls and/or .txt).

In Firefox it is doing the following on click on the link :

1. For .doc files, it opens the files (no File Download pop-up)
2. For .xls files, the File Download pop-up is seen however the file extension is not taken as .xls (Please see the attachment)

FileController Class

public class FileController extends AbstractFormController{
SearchResultService searchResultService;

public void setSearchResultService(SearchResultService searchResultService){
this.searchResultService = searchResultService;

protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
ResultSet result = searchResultService.getAttachment(Integer.parseInt(arg0.getParameter("prbId")));
byte[] buff = new byte[100000];
try {;
String filename = "fname."+result.getString("file_ext");
InputStream is = result.getBinaryStream(1);
OutputStream out = arg1.getOutputStream();
int bytesRead;
while ((bytesRead = != -1) {
out.write(buff, 0, bytesRead);
arg1.setHeader("content-disposition", "attachment; filename="+filename);
}else if(result.getString("file_ext").equals("xls")||result.getString("file_ext").equals("xlsx")){
}else if(result.getString("file_ext").equals("pdf")){
arg1.setHeader("Content-Desposition","attachment; filename="+filename);
byte[] bytesGot = result.getBytes(1);
ServletOutputStream outs = arg1.getOutputStream();
} catch (SQLException e) {
return new ModelAndView();

JSP that calls FileController class
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import=""%>
<%@page import="java.util.List,;"%>
<%@taglib prefix="form" uri=""%>
<%@taglib prefix="spring" uri=""%>
String contextPath = request.getContextPath();
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>SearchResult Page</title>
<% Description desc = (Description)request.getAttribute("commandBean"); %>
<div style="border-style: solid; border-width: 3px; margin-top: 20px; margin-bottom: 20px; ">
<div style="background-color: #303030">
<table width="100%">
<th align="left"><font size="5px" color="#D1D1D1">Search Result(s)</font></th>
<div style="margin-bottom: 10px; margin-left: 5px; margin-top: 10px">
<form:form method="POST" commandName="commandBean" id="searchForm">
<table border="1" bordercolor="#424242" width="980" height="200">
<th align="left" bgcolor="#8F8F8F" colspan="1"><font size="4px" style="font-family: sans-serif">Problem Statement </font></th>
<% if(desc.getProblemId()>=0 ){ %>
<td colspan="3"><%=desc.getProblemText().trim() %></td>
<%} %>
<th align="left" bgcolor="#8F8F8F" colspan="1"><font size="4px" style="font-family: sans-serif">Solution</font></th>
<% if(desc.getProblemId()>=0){ %>
<td colspan="3"><pre><%=desc.getSolutionText().trim() %></pre></td>
<%} %>
<th align="left" bgcolor="#8F8F8F" colspan="1"><font size="4px" style="font-family: sans-serif">External Link</font></th>
<% if(desc.getProblemId()>=0){
if(desc.getExternalLinkList().size() > 0){
<td colspan="3">
<% for(int k=0;k<desc.getExternalLinkList().size();k++){ %>
<font size="2" style="font-family: sans-serif"><a href="http://<%=desc.getExternalLinkList().get(k).trim() %>"><%=desc.getExternalLinkList().get(k) %></a></font><br/>
<%}%> </td><%} }%>
<% if(desc.getProblemId()>=0){
<th align="left" bgcolor="#8F8F8F" colspan="1"><font size="4px" style="font-family: sans-serif">Attachement</font></th>
<td><a href="<%=contextPath%>/<%=desc.getProblemId() %>">Download File</a></td>
<%}} %>
<td colspan="4"><br/><a href="<%=contextPath%>/">Back</a><br/></td>


All the extensions are saved/retrieved in the db correctly.

Please help.

[Thumbnail for SpreadSheet.JPG]

Atul Kotwale

Joined: Dec 22, 2011
Posts: 17
It seems that problem is related to browser and nothing to do with spring. Try to find it in firefox documentation.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

You might also have better luck getting a answer to this question in the Servlet/javascript forum since this question keeps popping up from time to time. You might try to search through those forums too for similar issues.
(You might request a moderator to move this question or probably ask a new question and link to this post. Duplicate questions aren't allowed @ the ranch)

Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Greg Funston
Ranch Hand

Joined: Feb 09, 2011
Posts: 81

Interface MultipartHttpServletRequest getFile MultipartFile getContentType should help. It returns mime type which should help resolve your browser issue and simplify your code.

Greg Funston
Don't get me started about those stupid light bulbs.
subject: File Download via Spring does not work in Firefox