Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Claudia Vaz
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic