-2

Generate PDF file in an appropriate format I asked here about how to prompt the PDF to be saved in its response.

Now I would like to make the file save at a predetermined directory on the container's file system (say D:\pdf on windows). So if I understand correctly instead of ServletOutputStream I would need a FileOutputStream.

So I think I would need to bypass writing to ServletOutputStream and use File IO. What do you think about my approach? Any advice would be much obliged!

Edit: I was a bit confused as to how streams work. And in general about the servers local file system. Anyways I appreciate your bearing with me and making my confusion go away

Community
  • 1
  • 1
yoda
  • 539
  • 1
  • 12
  • 32

3 Answers3

2

You have no control over where it will be saved on the user's computer. Imagine a servlet writing files to system directories and overwriting system files.

FileFooStream will write to the server's local disk. It should work fine, as long as the server allows file access(App Engine does not), and will write similarly to a desktop application.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
  • I meant server's local disk in my post. Will edit – yoda Sep 05 '13 at 16:16
  • 2
    Then this question is quite strange, because it's absolutely not different in a servlet from how you would do in a "regular" Java class. – BalusC Sep 05 '13 at 16:31
1

As far I know, you can not manipulate client directory through a web program. This is where browser's security comes into picture; also JVM sandbox would not allow any applet to execute with suspicious activities.

The other approach is to let client decide on what to do with the pdf file. For that you could use below step:

  1. Use Mime type in the response header to tell browser that you are going to write a PDF on the output stream.
  2. The browser would accept your byte and try to display it using a plugin like Adobe PDF reader (inbuilt in the browser).
  3. If the browser does have the plugin, it would let the user decide on what to do with this file (save/open prompt).
  4. Then user could decide on how to open the PDF file.

You could take a clue from this post on how to do this.

Community
  • 1
  • 1
Gyanendra Dwivedi
  • 5,511
  • 2
  • 27
  • 53
1

Looking into the question, I guess you are little confused the way Servlet works.

See servlet is Java class which is executed on a JVM on the server side. What you get from the Request and what you set into Response is a way for you to communicate with underlying HTTP server, which will finally be using the attributes from your Request and Response to generate a text, which it can transmit it to client browser.

So the File Input/Output stream which you will be creating in a Servlet will give you access to the directory of the machine where this servlet is being executed. And here it will be executed at your server. So at best you can cantrol, where an incoming file should be saved on a server using File Input/Output stream but there is no way you can get access to client machine directory using Servlet.

P.S. I have not considered the possibility of sending applet to browser.

mawia
  • 9,169
  • 14
  • 48
  • 57