• 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

Download file from Servlet via Applet

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
maybe someone can help me on this problem:
i'm trying to send a file from a servlet back to an applet, so
that the user on the client side can download it.
i think the code on the server-side is not the problem, because
if i use html as the front-end, i can download the file.
I'm using this code to connect to the servlet:

URL url=new URL(getCodeBase(), "/servlet/FileServlet");
URLConnection connection=url.openConnection();
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
ByteArrayOutputStream bs=new ByteArrayOutputStream(512);
PrintWriter out=new PrintWriter(bs,true);
String val=URLEncoder.encode("bar");
String data="foo="+val;
out.print(data);
out.flush();
connection.setRequestProperty("Content-Length",String.valueOf(bs.size()));
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
bs.writeTo(connection.getOutputStream());
On the server-side i use this code to send back the file:
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
ServletOutputStream out = res.getOutputStream ();
res.setContentType( "application/pdf" );
String fileURL ="file:///c:/test.pdf";
res.setHeader("Content-disposition","attachment; filename=test.pdf" );
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
URL url = new URL(fileURL );
bis = new BufferedInputStream(url.openStream());
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch(final MalformedURLException e) {
System.out.println ( "MalformedURLException." );
throw e;
} catch(final IOException e) {
System.out.println ( "IOException." );
throw e;
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
}

I know, that i have to use an inputstream on the client-side to get data from the servlet, but if ever used one, i could send it to an textarea or something like that, but i never were able to download it as a file.
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"fodor",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please choose a new name which meets the requirements.
Thanks.
reply
    Bookmark Topic Watch Topic
  • New Topic