2013年12月18日星期三

How to get JAVA download dialog box to save the path

Click on a page to download , the download dialog box pops up , select a custom path , how to get the absolute path JAVA ,
need to get the absolute path of the file to which to write the contents
similar : D: \ \ text

------ Solution ------------------------------------ --------


downloaded file is saved to the client put the computer you write will be written to the file server hard disk
------ Solution ------------ --------------------------------
you brush out the data directly on the line here, I have a jsp download zip package before you look

String path = application.getRealPath("/");
Map map = (Map)request.getAttribute("map");
String src = (String)map.get("zipSrc");
String filename = (String)map.get("zipName");
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader( "Content-Disposition;charset=GBK ","attachment; filename=\"" + filename + "\""); 
//File file = new File("D://五医院费用明细表.xls"); 
File file = new File(src); 
FileInputStream in = new FileInputStream(file); 
byte[] buffer = new byte[in.available()]; 
in.read(buffer); 
response.getOutputStream().write(buffer); 
response.getOutputStream().flush(); 
out.clear(); 
out = pageContext.pushBody();

------ Solution ------------------------------------- -------
I looked for so long, still do not understand ! What you really need to do ?
difficult to choose the path you are first and then write the data in it?
This is absolutely unreasonable.
IE 's download dialog box , you can not go to the Pro button to listen to something ,
So, want the choice of path selection , it is impossible !
Like Thunder dialog box , you can get to , but a lot of trouble !
page above the need to install Activex control ......
------ Solution ------------------------ --------------------
What is that document?
response.setContentType ("application / msexcel; charset = UTF-8");
response.reset () ;/ / clear the data buffer in
response.setHeader ("Content-disposition", "attachment; filename =" + URLEncoder.encode (filename, "UTF-8"));
response.flushBuffer (); / / send the buffer all the data to the client
So be it!
I do not know how you get, so pop-up dialog box Yeah !
------ Solution ---------------------------------------- ----

help you one last , http://blog.csdn.net/yfhdsz/archive/2006 / 09/01/1154491.aspx
------ For reference only -------------------------- -------------
this does not require you to control the browser to help you deal with the
download the files you just need to provide connectivity on the page
------ For reference only --------------------- ------------------
to get the path , custom file name , the background needed to write data to a file
------ For reference only - --------------------------------------
for security reasons , Web server can not write files directly to the customer's computer through the pages , the browser must intermediary
------ For reference only ----------------------- ----------------

+1
------ For reference only ------------- --------------------------
you just need to download the file into a directory on your server , and then some pages give users a download link ( the link to point directly to the file you )
------ For reference only ----------------------- ----------------
this.response.setContentType ("application / vnd.ms-excel");
this.response.addHeader ("Content-Disposition", "attachment; filename =" + new String ("text" getBytes ("GBK"). , "ISO-8859-1"));
this.response.setCharacterEncoding ("GBK");
this.writer = this.response.getWriter ();
/ / write data to text.xls Lane
writeExcel ("D :/ / text", colName, ds);

Here path is hardcoded , placed in the D drive after saving , and are now trying to save through the pop-up dialog box , select the path and file name as an argument put to
----- - For reference only ---------------------------------------
ask .. < br> ------ For reference only ---------------------------------------
/ **
* registration page special business logic
* /
public void processLogic ()
throws Exception
{


String name = request.getParameter ("tableName");
try
{
this.response.setContentType ("application / vnd.ms-excel");
this.response.addHeader ("Content-Disposition", "attachment; filename =" + new String ( name.getBytes ("GBK"), "ISO-8859-1"));
this.response.setCharacterEncoding ("GBK");
} catch (Exception e) {
throw new RuntimeException (e);
}
String sql = "select * from" + name;
Datastore ds = dc.retrieve (sql);
int colnum = ds.columnCount ();
String [] colName = new String [colnum];
for (int i = 0; i {
colName [i] = ds.getColumnName (i +1);
System.out.println (" first " + i + " times is =" + colName [i]);
}

writeExcel (name, colName, ds);


}


public static void writeExcel (String fileName, String [] fieldName, Datastore res)
{
FileOutputStream fos = null;
try {
fos = new FileOutputStream (fileName + "xls.");
HSSFWorkbook wb = new HSSFWorkbook ();
wb.setSheetName (0, " cost " ) ;
HSSFSheet s = wb.createSheet ();
createTag (fieldName, s) ;/ / write head of the table
try
{
createValue (res, s) ;/ / get the data set, and then get the data and write files
}
catch (SQLException e1)
{
e1.getStackTrace ();

}
catch (InvalidRowColException e2)
{
e2.getStackTrace ();
}
wb.write (fos);
fos.close ();
} catch (FileNotFoundException e) {
e.printStackTrace ();
} catch (IOException e) {
e.printStackTrace ();
}
finally
{
if (fos! = null)
{
try {
fos.close ();
} catch (IOException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
}
}
}

private static void createTag (String [] tags, HSSFSheet s)
{
HSSFRow row = s.createRow (0);
HSSFCell cell = null;

for (int i = 0; i {
cell = row.createCell ((short) i);
cell.setCellType (HSSFCell.CELL_TYPE_STRING);
cell.setEncoding (HSSFCell.ENCODING_UTF_16);
cell.setCellValue (tags [i]);
}

}

private static void createValue (Datastore res, HSSFSheet s) throws SQLException, InvalidRowColException
{
int flag = 1;
int count = res.columnCount ();
HSSFRow row = null;
HSSFCell cell = null;
for (int i = 1; i <= res.rowCount (); i + +)
{
row = s.createRow (flag);
for (int j = 1; j <= count; j + +)
{
cell = row.createCell ((short) (j-1));
Object obj = res.getObject (i, j);
cell.setCellType (HSSFCell.CELL_TYPE_STRING);
cell.setEncoding (HSSFCell.ENCODING_UTF_16);
cell.setCellValue (obj + "");
}
flag + +;
}

}
------ For reference only --------------------------------- ------
specified directory , check the data can be directly put the
------ For reference only ------------------- --------------------

you can operate the server that the client is really a
------ For reference NB only ---------------------------------------
alexandertech:

how to insert the data files stored on the client computer

before the code is already generated in the specified directory before the pop-up dialog . xls file , and then save the words are 0KB . xls
------ For reference only ----- ----------------------------------
how to write data to a file stored in it
------ For reference only ---------------------------------------
how to write data to a file stored in it
------ For reference only --------------------------- ------------
direct written response output stream on the line , save a browser to download their own thing , you do not get the tube can not pipe
--- --- For reference only ---------------------------------------
that in how to modify the above code does
------ For reference only ------------------------------- --------
  The reply was deleted at the moderator 2011-06-15 16:16:46

------ For reference only ---------------------------------- -----
18 F, you can also experience the same problem you
------ For reference only --------------------- ------------------
written response output stream to how to write it
------ For reference only ------- --------------------------------
Excel by its own customized
----- - For reference only ---------------------------------------
originally wanted to achieve B / S structure of the next generation excel file on the client , it was proposed that the first generation excel file on the server side , and then the excel file into outputStrean flow through the response to its output to the client, so the client will be prompted to "Open or Save for " we can achieve it. Now I have generated excel file , but later how, which give specific advice. My usage of the response is really an idiot , assuming excel file named "test.xls".
------ For reference only ------------- --------------------------
download what you do or do ... ?
response.flushBuffer ()
This method is to buffer the data sent to the client . . .
------ For reference only -------------------------------------- -
do download
------ For reference only -------------------------------- -------
I try response.flushBuffer ()

------ For reference only ---------------------------------- -----
download it first, you have created an excel file , and then writes the data to the inside , and finally IE pop-up dialog box for users to download , the user downloads in there has nothing to do with you Yeah !
------ For reference only -------------------------------------- -
excel file has been created , the data is also written into the final document download dialog box pops up IE is not really where I write data to a file
------ For reference only ----- ----------------------------------
this.response.setContentType ("application / vnd.ms -excel ");
this.response.addHeader ("Content-Disposition", "attachment; filename =" + new String (name.getBytes ("GBK"), "ISO-8859-1"));
this.response.setCharacterEncoding ("GBK");
/ / text.xls generated files , write data to the file
writeExcel ("D :/ / text", colName, ds);


D drive has generated text.xls not file before the dialog box , and there is data
pop-up dialog and then save it and generate a 0KB file a
------ For reference only -------------------- -------------------
why you want to write to the D drive to go ?
Just output.write () to write the data, using my code above ! You can,
need to write in the D drive.
Your problem is that your stream is written above !
------ For reference only -------------------------------------- -
specific point you just do not know how output.write () how to write
------ For reference only ------------------ ---------------------
 looked upstairs reply mess . Download the file save box that pops up is a browser thing , the server just write data to the stream which would be finished .

response.setContentType ("application / msexcel; charset = UTF-8");
response.reset () ;/ / clear the data buffer in
response.setHeader ("Content-disposition", "attachment; filename =" + URLEncoder.encode (filename, "UTF-8"));

response.getOutputStream () write (your file);.
------ For reference only --------------------- ------------------
/ / method calls
public OutputStream writeExcel (String fileName, String [] fieldName, Datastore res)
{
OutputStream fos = null;
fos = new FileOutputStream ("text" + "xls.");
HSSFWorkbook wb = new HSSFWorkbook ();
HSSFSheet s = wb.createSheet ();
createTag (fieldName, s) ;/ / write head of the table
createValue (res, s) ;/ / get the data set, and then get the data and write files
wb.write (fos);
fos.close ();
return fos;
}

this.response.setContentType ("application / vnd.ms-excel");
this.response.setHeader ("Content-Disposition", "attachment; filename =" + new String ("text" getBytes ("GBK"). , "ISO-8859-1"));
this.response.setCharacterEncoding ("GBK");

Is that right
OutputStream output = writeExcel (name, colName, ds);
response.getOutputStream () write (output);. / / output parameters can not be put
response.flushBuffer (); There seems to be a problem




------ For reference only ---------------------------------- -----
server and then want to get the client to write something ? If so , then the virus is not crazy ?
not , as mentioned above , with the establishment of a good flow to your excel file sent to the browser on the server , it 'll help you do it .
------ For reference only -------------------------------------- -
how to write data in output.write () in ah
has code specific point you
------ For reference only ---------------------------- -----------

there are two issues here :
1, you can not use FileOutputStream, because it does not put the data sent to the client , we wanted to change : OutputStream out = response.getOutputStream ();
2, wb.write (fos); it changed wb.write (out);
------ For reference only --------------- ------------------------


lz I want to ask you now to solve this problem yet?
I also want to be something that has a button on the page , when submitted , pop up a download dialog box for users to choose , then the procedure to obtain the path selected by the user , in users created on a local hard disk space to store the data obtained from the data to go inside , I also have to do now , is to find a way to bring up a dialog box to obtain the path selected by the user , and then the data into them, I I read your post , I feel that we are almost done , would like to ask you, how are you doing ? etc. Reply
------ For reference only -------------------------------- -------
this function I have achieved , need to add my QQ: 972415920
------ For reference only -------------- -------------------------

upstairs QQ verification is Shane ?
------ For reference only -------------------------------------- -
seeking validation qq problem
------ For reference only ------------------------------ ---------
QQ verification is not tangled
------ For reference only --------------------- ------------------
/ / declare a stream object
FileOutputStream fos = null;
Date now = new Date ();
/ / Default Default Style Language ( Chinese ) under (MEDIUM style , such as :2008 -6-16 20:54:53 )
DateFormat d1 = DateFormat.getDateInstance ();
/ / download file is displayed to the user 's name
String fileName = d1.format (now) + now.getSeconds () + "IKBK_Sources.txt";
try {
/ / create a stream object
fos = new FileOutputStream ("F: \ \ IKBK_Sources \ \" + fileName);
/ / convert byte array
byte [] b1 = sstart.getBytes ();
/ / write to file newline character sequence
byte [] b2 = "\ r \ n" getBytes ().;
fos.write (b1);
fos.write (b2);
/ / questions content
List ThemeList = biz.getThemeList (null);
for (Theme theme: ThemeList) {
fos.write (b2);
byte [] title = (theme.getTitle () + "()" + "\ r \ n") getBytes ().;
byte [] OptionA = (theme.getOptiona () + "\ r \ n") getBytes ().;
byte [] OptionB = (theme.getOptionb () + "\ r \ n") getBytes ().;
byte [] OptionC = (theme.getOptionc () + "\ r \ n") getBytes ().;
byte [] OptionD = (theme.getOptiond () + "\ r \ n") getBytes ().;
if (theme.getOptione () = null |! | theme.getOptionf () = null!) {
byte [] OptionE = (theme.getOptione () + "\ r \ n") getBytes ().;
byte [] OptionF = (theme.getOptionf () + "\ r \ n") getBytes ().;
fos.write (title);
fos.write (OptionA);
fos.write (OptionB);
fos.write (OptionC);
fos.write (OptionD);
fos.write (OptionE);
fos.write (OptionF);
} else {
fos.write (title);
fos.write (OptionA);
fos.write (OptionB);
fos.write (OptionC);
fos.write (OptionD);
}
}
fos.write (b2);

没有评论:

发表评论