Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Download pdf file in struts2

 
Arun Ak
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Arun Ak
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a binary file ( a png) and i used Fileoutputstream instead of ServletoutputStream. Will it make a difference?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic