jQuery in Action, 2nd edition*
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
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
Author

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 folder.how can i do that.

please help me out.

Thanks in Advance

Sreenivas
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Jan 09, 2005
Posts: 125
Hi

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

code
------------

String outputFile="E:/one.xls";
//String outputFile="one.xls";
if(action.equals("CreateExcelFile")){

try{
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);

cell.setCellValue("Product");

// Or do it on one line.
row.createCell((short)1).setCellValue("Month");
row.createCell((short)2).setCellValue("Sales");

rs=stmt.executeQuery(sql);

int i=1;
while(rs.next()){

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)0).setCellValue(rs.getString("Product"));
nrow.createCell((short)1).setCellValue(rs.getString("Month"));
nrow.createCell((short)2).setCellValue(new Integer(rs.getString("Sales")).intValue());
i++;

}

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



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

connMgr.freeConnection("sjo", con);

}
sangram mhatre
Greenhorn

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 .

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

os.flush();
os.close();
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: http://aspose.com/file-tools
 
subject: how to store a file in the local hard disk
 
Similar Threads
excel charts
how to write comma in CSV file
excel page not opening in a browser
Exporting data to excel file... using java web app
Problem on opening .XLSX file