File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Download pdf file in struts2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Download pdf file in struts2" Watch "Download pdf file in struts2" New topic
Author

Download pdf file in struts2

Arun Ak
Greenhorn

Joined: Jun 21, 2009
Posts: 21
My requriement.
1. open file dailog to select the file for save (from jsp).
2. after selecting the file name I create pdf file (it works fine for me).
3. save the created file to the location as per step 1.

currently I user <input type="file name="fileName />

can any body help me to getting real file path like c://src/tem/download.pdf on select the file name.

Thanks.
Arun.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

So you want to find the path on the client machine of the uploaded file in a struts action?? AFAIK, that's not possible. Even for security reasons you cannot get the full path of the file in javascript in many browsers...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Arun Ak
Greenhorn

Joined: Jun 21, 2009
Posts: 21
No ,I just to want to save the created pdf file to the user selected location.
my application takes input from user about file name (to write contenet) and location to save the file.

user must provide input through fileDailog

Regards,
Arun
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Do you want to save the file on the server or on client machine?? If you want to save the file on client machine, then you cannot control where the file will be saved. When the browser is asked to download a file, it prompts the user about where to store the file. You can control the name of the file using the content-disposition header...
Arun Ak
Greenhorn

Joined: Jun 21, 2009
Posts: 21
I wanted to save the file on client machine.
can you please give me some sample code of action and jsp.

Regards,
Arun
Arun Ak
Greenhorn

Joined: Jun 21, 2009
Posts: 21
Ankit Garg wrote:Do you want to save the file on the server or on client machine?? If you want to save the file on client machine, then you cannot control where the file will be saved. When the browser is asked to download a file, it prompts the user about where to store the file. You can control the name of the file using the content-disposition header...


Can you please let me know how can I get actual selected file path on client machine.
1. open FileDailog - user select file, and click ok, in my jsp I get some long file path which is not real path.

For e.g
C://temp/text.pdf - selected by user now I want same path in my action class from jsp.

Regards,
Arun
Arun Ak
Greenhorn

Joined: Jun 21, 2009
Posts: 21
Hello All

My application implemented with two clients swing and struts2

In swing client - when user click on pdf button it opens File chooser using JFileChooser- user select file [e.g c://temp/result.pdf] and on click of open, pdf file created in that location.

Same way I want to implement it in my struts2 web client. on click of pdf button open file chooser , on click of open create pdf file in that location.

My problem in web client is not able to get user selected file path e.g c://temp/result.pdf, please help me.

Regards,
Arun
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

I'm not sure why you are having problem with this. User clicks on pdf button, the request goes to the server, the server sends the pdf file as an attachment, the browser asks the user where to save the file, the user selects a location and the download starts on the location. Why do you need to send the location to the server?? The server will just send the file and the user will then select the location to save the file. Example on how to force browser to save the pdf file can be found here (it simply uses the content-disposition header that I pointed out before), the page actually uses a JSP to do all the scripting code which is not very good, but the logic is correct...
Arun Ak
Greenhorn

Joined: Jun 21, 2009
Posts: 21
Hi Ankit Garg,

Create pdf method takes one of input as File (Selected file name along with path). then it start writtin pdf content as

public void createAndSavePdf(String targetFileNameWithPath) {

Document document = new Document(PageSize.A4, 50, 50, 100, 72);
PdfWriter writer1 = PdfWriter.getInstance(document, new FileOutputStream(targetFileNameWithPath));
document.open();
----
----
---
document.close();

}

Regards,
Arun
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Arun, you can't write files to client machine like that. You'll have to write the file to the ServletOutputStream. You can only control the name of the file. You'll have to modify your code somehow to work with both the Swing and Struts app...
Arun Ak
Greenhorn

Joined: Jun 21, 2009
Posts: 21
Ankit Garg wrote:Arun, you can't write files to client machine like that. You'll have to write the file to the ServletOutputStream. You can only control the name of the file. You'll have to modify your code somehow to work with both the Swing and Struts app...


Ok, If file is too large, do you think is it fessaible to write contenet into ServletOutputStream if not can you let me know alternative soluation.

Regards,
Arun
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

There is no alternative. It doesn't matter what the size of your file is, if its a binary file, you'll have to use ServletOutputStream, if its a text file, you can use PrintWriter...
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
I have a binary file ( a png) and i used Fileoutputstream instead of ServletoutputStream. Will it make a difference?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Download pdf file in struts2