• 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

How to download the file to the client machine

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

In my project i need to download the file to the client machine from my server, Below is the code, its downloading the file but saving the file in the server itself. I want it to save in the client machine. Pls help me out guys. Im not getting where im going wrong.

<%@ page language="java"
import="java.lang.*,java.io.*, com.utils.*,
java.sql.*,
listener.*,java.net.URL,java.net.URLConnection" %>

<%@ include file="Connection.jsp" %>



<%

String path = request.getContextPath();

String basePath =
request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";



// Check for valid session

if (session.getAttribute("logged") ==
null)
response.sendRedirect("../htmls/InvalidSession.html");

%>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN">

<HTML>

<HEAD>

<link rel="stylesheet"
type="text/css" href="../styles/Portal.css">

</HEAD>

<BODY>

<%

String repoDir =
request.getParameter("repoDir");

String fileName =
request.getParameter("fileName");

String docId =
request.getParameter("fileId");

String folderName =
request.getParameter("folderName");

%>

<%

String docPath = "";

String outdocPath = "";

DataInputStream in=null;

DataOutputStream ffout=null;

FileOutputStream fOut=null;


docPath="http://webdevelop-5:8080/Maddy/" + repoDir +
"/" + folderName + "/" + fileName;

//basePath + repoDir + "/" +
folderName + "/" + fileName;

try

{




URL remoteFile=new URL(docPath);


URLConnection
fileStream=remoteFile.openConnection();


// Open the input streams for the
remote file


fOut=new
FileOutputStream("c://"+fileName);




// Open the output streams for saving
this file on disk


ffout=new DataOutputStream(fOut);


in=new
DataInputStream(fileStream.getInputStream());


int data;


while((data=in.read())!=-1){


fOut.write(data);


}


in.close();


fOut.flush();


fOut.close();





}

catch(Exception e)

{

%>

<jsp:forward
page="MessagePage.jsp">


<jsp aram name="mess" value="<%=e.toString()%>" />


<jsp aram name="messType" value="Error" />


<jsp aram name="title" value="Error Message" />

</jsp:forward>

<%

}

%>

<a href ='mailto:?subject=Company
news&attachments=""c:\<%=fileName%>""'>email</a>

</BODY>

</HTML>
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JSP pages run on the server. Whatever code you put in there will not be able to access the client (unless the client hard drive is specifically mapped and made available to the server, but that is not the case in general).

Can't you put a link to the file on the HTML page you're generating, so that the user can click on it, and save the file wherever she wants on the local machine?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
madhavi,

Javaranch tip:
If you are going to post more than a line or two of your code, wrap the code in UBB Code tags. Doing so will preserve your indenting, making your code much easier to read. Making it easier to read makes it more likely that people will actually read it which makes it more likely that you will get help.
 
madhavi prasad
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thnx for the tip. I will try to follow from now can.
can any one giv me sample code whoch file save the file from server to the client machine.

Thnx
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't save it directly on the client machine.
The best you can do is send it to the client in a way that will put up a dialog box prompting them to save it.

They then get to choose where to save the file. You have no control over that part of the process (it would be a security hole)

This sort of thing is done best through a servlet.
Basically you do what you are doing here, but instead of writing to a FileOutputStream, write to a ServletOutputStream, gained by calling response.getOutputStream.
The sending of the file would count as the response, so you can't show an HTML page at the end of downloading the file (1 request = 1 response)
 
madhavi prasad
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But i cant do that way.
The requirement is some thing like this.
Im quering some values from the database and showing them in the table format in a jsp page. One of the column is download img. When i click on that I need to download the file from the server and place it on the client machine, sec task is after completion of first task that is savin gto the client machine it has to open the Microsoft Outlook and that file should as the attachment(Here user dont hav to attach the file.).

How do i achive this .
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your requirements are impossible to achieve.

Without resorting to something like signed Applets or ActiveX controls you cannot -- repeat cannot -- write any file to the client system without going through the normal dowload process described by others in this thread. Imagine the security problems if this were not the case!
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by madhavi prasad:
sec task is after completion of first task that is savin gto the client machine it has to open the Microsoft Outlook and that file should as the attachment(Here user dont hav to attach the file.).



Sounds non-sense to me. May be I didn't get you.
 
reply
    Bookmark Topic Watch Topic
  • New Topic