File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File Download via Spring does not work in Firefox

 
ShruthiJ Rao
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

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;
}


@Override
protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
ResultSet result = searchResultService.getAttachment(Integer.parseInt(arg0.getParameter("prbId")));
byte[] buff = new byte[100000];
try {
result.next();
if(!(result.getBinaryStream(1)==null)){
String filename = "fname."+result.getString("file_ext");
if(result.getString("file_ext").equals("txt")){
InputStream is = result.getBinaryStream(1);
OutputStream out = arg1.getOutputStream();
arg1.reset();
int bytesRead;
while ((bytesRead = is.read(buff)) != -1) {
out.write(buff, 0, bytesRead);
}
arg1.setContentType("");
arg1.setHeader("content-disposition", "attachment; filename="+filename);
is.close();
out.flush();
out.close();
}else{
arg1.reset();
if(result.getString("file_ext").equals("doc")||result.getString("file_ext").equals("docx")){
arg1.setContentType("application/msword");
}else if(result.getString("file_ext").equals("xls")||result.getString("file_ext").equals("xlsx")){
arg1.setContentType("application/vnd.ms-excel");
}else if(result.getString("file_ext").equals("pdf")){
arg1.setContentType("application/pdf");
}
arg1.setHeader("Content-Desposition","attachment; filename="+filename);
byte[] bytesGot = result.getBytes(1);
ServletOutputStream outs = arg1.getOutputStream();
outs.write(bytesGot);
outs.flush();
outs.close();
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return new ModelAndView();
}
}



JSP that calls FileController class
----------------------------------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="com.ibm.tool.command.Description"%>
<%@page import="java.util.List, com.ibm.tool.command.Keywords;"%>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%
String contextPath = request.getContextPath();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>SearchResult Page</title>
</head>
<body>
<% 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%">
<tr>
<th align="left"><font size="5px" color="#D1D1D1">Search Result(s)</font></th>
</tr>
</table>
</div>
<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">
<tr>
<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>
<%} %>
</tr>
<tr>
<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>
<%} %>
</tr>
<tr>
<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><%} }%>
</tr>
<% if(desc.getProblemId()>=0){
if(desc.getExtension()!=null){
%>
<tr>
<th align="left" bgcolor="#8F8F8F" colspan="1"><font size="4px" style="font-family: sans-serif">Attachement</font></th>
<td><a href="<%=contextPath%>/file.do?prbId=<%=desc.getProblemId() %>">Download File</a></td>
</tr>
<%}} %>
<tr>
<td colspan="4"><br/><a href="<%=contextPath%>/search.do?keepSearch=true">Back</a><br/></td>
</tr>
</table>

</form:form>
</div>
</div>
</body>
</html>


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

Please help.



SpreadSheet.JPG
[Thumbnail for SpreadSheet.JPG]
 
Atul Kotwale
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 607
Firefox Browser Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Greg Funston
Ranch Hand
Posts: 81
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interface MultipartHttpServletRequest getFile MultipartFile getContentType should help. It returns mime type which should help resolve your browser issue and simplify your code.

Cheers,
Greg Funston
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic