This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Servlets and the fly likes how to store a file in the local hard disk Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to store a file in the local hard disk" Watch "how to store a file in the local hard disk" New topic

how to store a file in the local hard disk

sreenivas jeenor
Ranch Hand

Joined: Jan 09, 2005
Posts: 125
Hi All,

I am writing the Servlet code to generate an excel file form the database.
i am using fixed location to save the excel file created.

what i required is the user to have option of storing in a particular can i do that.

please help me out.

Thanks in Advance

David O'Meara

Joined: Mar 06, 2001
Posts: 13459

Sorry, which part is causing you issues?
sreenivas jeenor
Ranch Hand

Joined: Jan 09, 2005
Posts: 125

In the below code i am using outputFile as fixed Location.what i want is the user must select his own location to store the file


String outputFile="E:/one.xls";
//String outputFile="one.xls";

connMgr = DBConnectionManager.getInstance();
con = connMgr.getConnection("sjo");
Debug.println("The connection is..."+con);
stmt = con.createStatement();
String sql="select product,month,sales from testdata";

// Create a New XL Document
HSSFWorkbook wb = new HSSFWorkbook();
// Make a worksheet in the XL document created
HSSFSheet sheet = wb.createSheet();

// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow((short)0);

// Create a cell and put a value in it.
HSSFCell cell = row.createCell((short)0);


// Or do it on one line.


int i=1;

System.out.println("i value in new row is........."+i);

HSSFRow nrow = sheet.createRow((short)i);

// Or do it on one line.
nrow.createCell((short)2).setCellValue(new Integer(rs.getString("Sales")).intValue());


// The Output file is where the xls will be created
FileOutputStream fOut = new FileOutputStream(outputFile);
// Write the XL sheet
// Done Deal..

}catch(Exception e) {
System.out.println("!!BANG!! xlCreate() : " + e );

connMgr.freeConnection("sjo", con);

sangram mhatre

Joined: Apr 02, 2005
Posts: 15
You can write your file to root directory of your web app .

and then respond back to your client as below ,
which will prompt the user to select the location to save his file .

InputStream is = ctx.getResourceAsStream("/<urfilename>");
int read = 0 ;
byte[] bytes = new byte[1024];
OutputStream os = response.getOutputStream();
while ((read = != -1) {
os.write(bytes, 0 ,read);

sreenivas jeenor
Ranch Hand

Joined: Jan 09, 2005
Posts: 125
Thanks sangram mhatre for your input.its working now
I agree. Here's the link:
subject: how to store a file in the local hard disk
jQuery in Action, 3rd edition