*
The moose likes Servlets and the fly likes download a file from the server an save it to the client disk Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "download a file from the server an save it to the client disk" Watch "download a file from the server an save it to the client disk" New topic
Author

download a file from the server an save it to the client disk

Claudia Vaz
Ranch Hand

Joined: Nov 13, 2001
Posts: 44
Hello
I would like to download a file from the server and save it to the client disk. But i don't know how. Can you help me!!!???
Thanks
Claudia
Claudia Vaz
Ranch Hand

Joined: Nov 13, 2001
Posts: 44
I found some code but it's not working perfectly!
In a jsp file i have
"<form enctype="multipart/form-data" method="post" action="save.jsp">
<input type="file" name="pdfurl">
<input type="submit" value="save">
</form>"
In the save.jsp i have the following code
"<jsp:useBean id="beanSalvar" scope="page" class="salvar" />
<%
beanSalvar.doPost(request,response);
%>"
In the salvar class i have the following code:
"import java.io.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletInputStream;
public class salvar
{
public void doPost( HttpServletRequest req, HttpServletResponse res)throws Exception, IOException
{
String pdf = req.getParameter("pdfurl");
String filename = pdf.substring(pdf.lastIndexOf("/")+1);
//res.setContentType("application/x-filler");
res.setContentType("application/txt");
res.setHeader("Content-Disposition","attachment;filename=" + filename );

try
{
FileInputStream fileInput = new FileInputStream(pdf);
int numOfBytes = fileInput.available();
byte byteArray[] = new byte[numOfBytes];
int nextByte = fileInput.read(byteArray);
fileInput.close();
OutputStream outStream = res.getOutputStream();
outStream.write(byteArray);
outStream.close();
}
catch(IOException ioe)
{
System.out.println("download:file input exception");//..........
//ioe.printStackTrace();
}
catch(Exception e)
{
System.out.println("download: exception");//..........
}
}
}
"
First he doesn't have the value of req.getParameter("pdfurl");
Second, if i give him the value he downloads the file, saves it but he gives the following error:
"2001-12-13 08:44:27 - Ctx( /PS2 ): IllegalStateException in: R( /PS2 + /save.jsp
+ null) OutputStream is already being used for this request"
Can anyone help?
Thanks
Claudia Vaz
Matts Smith
Ranch Hand

Joined: Feb 03, 2001
Posts: 113
from server to client? hmmm?
begin irony;
There is a real tricky way but it is coded in HTML. look
<a href="file.txt">click here</a>
as you can see it is not an easy task to do it
end irony;
if you mean client to server then go there for some tool http://servlets.com/cos/index.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: download a file from the server an save it to the client disk
 
Similar Threads
file download from JSP
Saving File to Client Machine.
Reading MDB file from a response
how to download or view the "Excel FILE "i n IE browser
Upload file from Client Browser