Download rtf doc to user selected location

Hi Team,

i have written a code to generate rtf, i have a servlet which generate the rtf document. but in my present code iam giving a hard coded location on which the rtf will be downloaded.

But now i want to give user to select the location on his harddisk.

Please help me how to do that?

[315 byte] By [aditya4ua] at [2007-11-27 5:05:59]
# 1
All you at least need to do is to set the content disposition of the response to 'attachment'. This will show a 'Save as' dialog. The downloadFile() snippets which you can find at the bottom of http://balusc.xs4all.nl/srv/dev-jep-pdf.html might be useful.
BalusCa at 2007-7-12 10:24:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Hi,

Here is my code, even after adding disposition iam not able to get what i require

import javax.servlet.http.HttpServlet;

import java.awt.Color;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.sql.SQLException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import com.lowagie.text.Chunk;

import com.lowagie.text.Document;

import com.lowagie.text.DocumentException;

import com.lowagie.text.Element;

import com.lowagie.text.Font;

import com.lowagie.text.Paragraph;

import com.lowagie.text.rtf.RtfWriter2;

import com.lowagie.text.rtf.field.RtfPageNumber;

import com.lowagie.text.rtf.headerfooter.RtfHeaderFooter;

import com.lowagie.text.rtf.style.RtfFont;

/**

* Servlet Class

*

* @web.servlet name="Generate"

*display-name="Name for Generate"

*description="Description for Generate"

* @web.servlet-mappingurl-pattern="/Generate"

* @web.servlet-init-paramname="A parameter"

*value="A value"

*/

public class Generate extends HttpServlet {

protected void doGet(HttpServletRequest req, HttpServletResponse resp)

throws ServletException , IOException {

// TODO Auto-generated method stub

HttpSession session = req.getSession(true);

Document document = new Document();

String locationToStoreDoc = "c:/MyDocument.doc";

try {

WHLProjectDAO dao = new WHLProjectDAO();

WHLProjectDTO dto = new WHLProjectDTO();

RtfWriter2.getInstance(document, new FileOutputStream(locationToStoreDoc));

document.open();

// Create a page Number

int indexValue = Integer.parseInt(session.getAttribute("indexValue").toString());

String getName = session.getAttribute("name").toString();

dto = dao.selectDB(indexValue);

if(!dto.equals(null)) {

String businessAccName = dto.getBusinessAccountName();

String preProClientName = dto.getPreProClientName();

String deliveryAccName = dto.getDeliveryAccountName();

String coeTrainingName = dto.getTrainingName();

String coePocName = dto.getPocsName();

String marketingName = dto.getMarketingTitle();

String othersTitle = dto.getTrainingName();

String peopleName = dto.getPeopleName();

String partnerName = dto.getPartnerName();

String marketName = dto.getMarketDescription();

Paragraph par = new Paragraph("Page ");

par.setAlignment(Element.ALIGN_RIGHT);

par.add(new RtfPageNumber());

RtfHeaderFooter footer = new RtfHeaderFooter(par);

document.setFooter(footer);

document.add(footer);

document.add(Chunk.NEWLINE);

RtfFont rtf = new RtfFont("Verdana",12,Font.BOLDITALIC );

rtf.setColor(Color.BLUE);

RtfFont lastUpdate = new RtfFont("Arial" ,10 , Font.BOLD);

lastUpdate.setColor(Color.BLACK);

lastUpdate.color().darker();

RtfFont sideHeading = new RtfFont("Verdana",12, Font.BOLDITALIC );

sideHeading.setColor(new Color(238 ,130 , 238));//238, 130 , 238

sideHeading.color().brighter();

RtfFont normal = new RtfFont("Verdana", 11 , Font.BOLD);

normal.setColor(new Color(238 ,130 , 238));

normal.color().brighter();

RtfFont normalText = new RtfFont("Arial" , 10 , Font.NORMAL);

normalText.setColor(Color.BLACK);

normalText.color().darker();

Paragraph para1 = new Paragraph(" HighLights For The Week From "+dto.getStartDate()+" To "+dto.getEndDate(),rtf);

para1.setAlignment(Element.ALIGN_CENTER);

document.add(para1);

Paragraph para = new Paragraph(" E-Enabling, EAI BPM Practice " , rtf);

para.setAlignment(Element.ALIGN_CENTER);

document.add(para);

document.add(Chunk.NEWLINE);

Paragraph para0 = new Paragraph(" Last Update By : "+getName , lastUpdate);

para.setAlignment(Element.ALIGN_RIGHT);

document.add(para0);

document.add(Chunk.NEWLINE);

if(!(businessAccName == null)) {

Paragraph para2 = new Paragraph(" Business Wins : " , sideHeading);

para2.setAlignment(Element.ALIGN_LEFT);

document.add(para2);

document.add(Chunk.NEWLINE);

Paragraph para3 = new Paragraph("Account Name : "+dto.getBusinessAccountName()+" ("+dto.getBusinessType()+")" , normal);

para3.setAlignment(Element.ALIGN_LEFT);

document.add(para3);

document.add(Chunk.NEWLINE);

Paragraph para4 = new Paragraph("Account Description : "+dto.getBusinessDescription() , normalText);

para4.setAlignment(Element.ALIGN_LEFT);

document.add(para4);

document.add(Chunk.NEWLINE);

}

if(!(preProClientName == null )) {

Paragraph para5 = new Paragraph(" Prospects /Pre-Sales : " , sideHeading);

para5.setAlignment(Element.ALIGN_LEFT);

document.add(para5);

document.add(Chunk.NEWLINE);

Paragraph para6 = new Paragraph(dto.getPreProClientName(),normal);

para6.setAlignment(Element.ALIGN_LEFT);

document.add(para6);

document.add(Chunk.NEWLINE);

Paragraph para7 = new Paragraph(dto.getPreProDescription(),normalText);

para7.setAlignment(Element.ALIGN_LEFT);

document.add(para7);

document.add(Chunk.NEWLINE);

}

if(!(deliveryAccName == null )) {

Paragraph para8 = new Paragraph(" Delivery highlights : " , sideHeading);

para8.setAlignment(Element.ALIGN_LEFT);

document.add(para8);

document.add(Chunk.NEWLINE);

Paragraph para9 = new Paragraph(dto.getDeliveryAccountName(),normal);

para9.setAlignment(Element.ALIGN_LEFT);

document.add(para9);

document.add(Chunk.NEWLINE);

Paragraph para10 = new Paragraph(dto.getDeliveryDescription(),normalText);

para10.setAlignment(Element.ALIGN_LEFT);

document.add(para10);

document.add(Chunk.NEWLINE);

}

if(!(coeTrainingName == null )) {

Paragraph para11 = new Paragraph(" Trainings : " , sideHeading);

para11.setAlignment(Element.ALIGN_LEFT);

document.add(para11);

document.add(Chunk.NEWLINE);

Paragraph para12 = new Paragraph(dto.getTrainingName() , normal);

para12.setAlignment(Element.ALIGN_LEFT);

document.add(para12);

document.add(Chunk.NEWLINE);

Paragraph para28 = new Paragraph("People Attending Training : "+dto.getTrainingAttendence() , normal);

para28.setAlignment(Element.ALIGN_LEFT);

document.add(para28);

document.add(Chunk.NEWLINE);

Paragraph para29 = new Paragraph("Training Faculty : "+dto.getTrainingFaculty() , normal);

para29.setAlignment(Element.ALIGN_LEFT);

document.add(para29);

document.add(Chunk.NEWLINE);

Paragraph para30 = new Paragraph("Training Location : "+dto.getTrainingLocation() , normal);

para30.setAlignment(Element.ALIGN_LEFT);

document.add(para30);

document.add(Chunk.NEWLINE);

Paragraph para13 = new Paragraph("Training Commnents : "+dto.getTrainingComments() , normalText);

para13.setAlignment(Element.ALIGN_LEFT);

document.add(para13);

document.add(Chunk.NEWLINE);

}

if(!(coePocName == null )) {

Paragraph para14 = new Paragraph(" PoCs/ Point Solutions : " , sideHeading);

para14.setAlignment(Element.ALIGN_LEFT);

document.add(para14);

document.add(Chunk.NEWLINE);

Paragraph para15 = new Paragraph("Pocs Location : "+dto.getPocsName() , normal);

para15.setAlignment(Element.ALIGN_LEFT);

document.add(para15);

document.add(Chunk.NEWLINE);

Paragraph para31 = new Paragraph("Pocs Status : "+dto.getPocsStatus() , normal);

para31.setAlignment(Element.ALIGN_LEFT);

document.add(para31);

document.add(Chunk.NEWLINE);

Paragraph para16 = new Paragraph(" Pocs Comments : "+dto.getPocsComments() , normalText);

para16.setAlignment(Element.ALIGN_LEFT);

document.add(para16);

document.add(Chunk.NEWLINE);

}

if(!(marketingName == null )) {

Paragraph para17 = new Paragraph(" COE Marketing : " , sideHeading);

para17.setAlignment(Element.ALIGN_LEFT);

document.add(para17);

document.add(Chunk.NEWLINE);

Paragraph para18 = new Paragraph(" Marketing Name : "+dto.getMarketingTitle() , normal);

para18.setAlignment(Element.ALIGN_LEFT);

document.add(para18);

document.add(Chunk.NEWLINE);

Paragraph para19 = new Paragraph(" Marketing Comments : "+dto.getMarketingDescription() , normalText);

para19.setAlignment(Element.ALIGN_LEFT);

document.add(para19);

document.add(Chunk.NEWLINE);

}

if(!(othersTitle == null )) {

Paragraph para20 = new Paragraph(" Others : " , sideHeading);

para20.setAlignment(Element.ALIGN_LEFT);

document.add(para20);

document.add(Chunk.NEWLINE);

Paragraph para21 = new Paragraph(" Others Name : " , normal);

para21.setAlignment(Element.ALIGN_LEFT);

document.add(para21);

document.add(Chunk.NEWLINE);

Paragraph para22 = new Paragraph(" Others Description : " , normalText);

para22.setAlignment(Element.ALIGN_LEFT);

document.add(para22);

document.add(Chunk.NEWLINE);

}

if(!(peopleName == null )) {

Paragraph para23 = new Paragraph(" People Highlights :" , sideHeading);

para23.setAlignment(Element.ALIGN_LEFT);

document.add(para23);

document.add(Chunk.NEWLINE);

Paragraph para24 = new Paragraph(" People Name : "+dto.getPeopleName() , normal);

para24.setAlignment(Element.ALIGN_LEFT);

document.add(para24);

document.add(Chunk.NEWLINE);

Paragraph para25 = new Paragraph(" People Achivements : "+dto.getPeopleAcheivement() , normalText);

para25.setAlignment(Element.ALIGN_LEFT);

document.add(para25);

document.add(Chunk.NEWLINE);

}

if(!(partnerName == null )) {

Paragraph para23 = new Paragraph(" Partner Highlights :" , sideHeading);

para23.setAlignment(Element.ALIGN_LEFT);

document.add(para23);

document.add(Chunk.NEWLINE);

Paragraph para24 = new Paragraph(" Partner Name : "+dto.getPartnerName() , normal);

para24.setAlignment(Element.ALIGN_LEFT);

document.add(para24);

document.add(Chunk.NEWLINE);

Paragraph para25 = new Paragraph(" Description : "+dto.getPartnerDescription() , normalText);

para25.setAlignment(Element.ALIGN_LEFT);

document.add(para25);

document.add(Chunk.NEWLINE);

}

if(!(marketName == null )) {

Paragraph para26 = new Paragraph(" Market Highlights : " , sideHeading);

para26.setAlignment(Element.ALIGN_LEFT);

document.add(para26);

document.add(Chunk.NEWLINE);

Paragraph para27 = new Paragraph(" Market Description : "+marketName , normalText);

para27.setAlignment(Element.ALIGN_LEFT);

document.add(para27);

document.add(Chunk.NEWLINE);

}

session.setAttribute("location", locationToStoreDoc);

resp.sendRedirect("../success.jsp");

document.close();

}

else {

}

} catch (FileNotFoundException fnfe) {

} catch (DocumentException de) {

}catch(SQLException se) {

}catch(ClassNotFoundException cnfe) {

}

}

}

aditya4ua at 2007-7-12 10:24:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

I don't think you got what BalusC was trying to say; you're hardcoding the location where the RTF file will be generated and that's fine. Remember, this path that you're giving will be the filesystem of the server machine.

The best you can do is give your client a 'Save As' dialog box where they can choose the download location ( I'm sure you've seen this in action ) and the way to do this is to set the headers in the response telling the browser you're sending a file of a particular type and that it would probably do well to let the user download it; and then write the contents of the file to the OutStream of the response.

nogoodatcodinga at 2007-7-12 10:24:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

If you are giving path in the servlet then it will be stored at server side not at client side.

If you wanna give the client an option to save into a desired location,

after generating the content , set the content type as rtf and then write the content into printstream.

Whether the content to be displayed in client browser or getting save dialog depends on client side browser settings.

I.e., if the rtf contenet is linked to MSword app in browser then it will be opened in browser as word document and user can save it. Other wise it will automatically give a dialog with options open,save and cancel.

sridhar_ratnaa at 2007-7-12 10:24:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...