Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Downloading file from a server

843840Sep 11 2002 — edited Dec 3 2002
Hi guys. I am attempting to allow a user to download or open a file in their browser via a servlet. I can successfully do this, providing the user has configured their browser image to viewer - so they can choose "Open" to view in the browser window, or "Save As" to save it locally.

**PROBLEM** The file (no matter what file is selected for download) - is given the name of the servlet instead of the actual file name, therefore when the windows file browser prompts for save, the file is named whatever I named the servlet. I am only dealing with .TIF images, so for example a server file named F01222.TIF can be saved, but locally it appears as viewfax.TIF, where viewfax is the name of my servlet.

Here is my code, any suggestions are appreciated. Thanks.


/**************************************************
*
* File name: IfaxViewFax.java
* Author: Geoff Peters, Logic Communications
* Date: March 22, 2002
*
* Directory: ROOT/webapps/IfaxSend/WEB-INF/classes
* Application : Ifax Web Fax
*
*************************************************** */


import replix.*;
import java.util.*;
import java.text.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;


public class IfaxViewFax extends HttpServlet

{

String FaxUserId = "";
String faxFileName = "";
String odbc = "";
String dbUser = "";
String dbPass = "";
String path = "";


public void init (ServletConfig cfg) throws ServletException {


super.init(cfg);
path = cfg.getInitParameter("realPath");

}

public void destroy(){

super.destroy();

}




public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
HttpSession session = request.getSession(true);
ServletOutputStream stream = response.getOutputStream();
FaxUserId = (String)request.getRemoteUser();


if(FaxUserId==null) {

stream.println("Not Logged In");
// not logged in }

else {
// logged in

session.setAttribute("FaxUserID", FaxUserId);

//Set variable passed in URL

faxFileName = request.getParameter("filename");

// Define actual path to server side file

String pathName = (path + FaxUserId + "/");

try {

String fileName= faxFileName ;
String filePath = pathName;
String downloadFile = filePath + fileName;

response.setContentType("image/tiff");
String download_date = "sysdate";

response.setHeader("Content-Disposition:","inline; filename=\"" + downloadFile + "\";");


BufferedInputStream fif = new BufferedInputStream(new FileInputStream(downloadFile));
int data;
while((data = fif.read()) != -1)
{
stream.write(data);
}
fif.close();
stream.close();
}

catch(Exception e) {
e.printStackTrace();
}

finally {
try{

} catch (Exception ignore) {}
}


}

} // end of doGet Method


} // End of IfaxViewFax class
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 31 2002
Added on Sep 11 2002
4 comments
299 views