When developing the B/S mode, the client needs to execute a program to download the information submitted by the client to the local machine. My idea is to generate a file on the server, download it to the client, and delete it. The function is implemented, but I always feel a little stubborn and I don't know how to solve it. below is my program:
Package chartreport;
Import org. Apache. Struts. Action .*;
Import javax. servlet. http .*;
Import java. util .*;
Import java. Io .*;
Import java.net .*;
Public class jbfilesave {
/**
*
* @ Param filename string
* @ Param response httpservletresponse
*/
Public void downloadattachment (string fileondisk, string filename,
Httpservletresponse response ){
// String filename = "test.txt ";
// System. Out. println ("... fileondisk =" + fileondisk );
// System. Out. println ("...... filename =" + filename );
// Convert to the actual address in the hard disk
Inputstream input = NULL;
Outputstream output = NULL;
File F = NULL;
Try {
Try {
Input = new fileinputstream (fileondisk );
F = new file (fileondisk );
}
Catch (ioexception e ){
// System. Out. println ("can not get attchment on disk ");
}
Byte [] buffer = getbytes (input );
Input. Close ();
Input = NULL;
Output = response. getoutputstream ();
Response. setcontenttype ("application/octet-stream ");
Response. setheader ("location", gbtoiso (filename ));
Response. setheader ("content-disposition ",
"Attachment; filename =/" "+ gbtoiso (filename) + "/"");
Response. setcontentlength (INT) F. Length ());
Output. Write (buffer );
Output. Flush ();
Output. Close ();
Output = NULL;
}
Catch (ioexception e ){
// System. Out. println ("error Download Attachment .");
}
Finally {
If (input! = NULL ){
Try {
Input. Close ();
}
Catch (ioexception ex ){}
}
If (output! = NULL ){
Try {
Output. Close ();
}
Catch (ioexception ex ){}
}
}
}
/**
*
* @ Param inputstream
* @ Throws ioexception
* @ Return byte []
*/
Public byte [] getbytes (inputstream) throws ioexception {
Bytearrayoutputstream = new bytearrayoutputstream (
1024 );
Byte [] block = new byte [512];
While (true ){
Int readlength = inputstream. Read (Block );
If (readlength =-1 ){
Break;
}
Bytearrayoutputstream. Write (Block, 0, readlength );
}
Byte [] retvalue = bytearrayoutputstream. tobytearray ();
Bytearrayoutputstream. Close ();
Return retvalue;
}
/**
*
* @ Param filename string
* @ Throws exception
*/
Public void deletefile (string filename) throws exception {
File file = new file (filename );
If (file. exists ()){
File. Delete ();
}
}
Public String changecode (string Str ){
String result = NULL;
Byte temp [];
Try {
Temp = Str. getbytes ("gb2312 ");
Result = new string (temp );
}
Catch (Java. Io. unsupportedencodingexception ex ){
// System. Out. println (ex. tostring ());
}
Return result;
}
Public String gbtoiso (string GB ){
String ISO;
Try
{
If (GB. Equals ("") | GB = NULL)
{
Return "";
}
Else
{
GB = GB. Trim ();
ISO = new string (GB. getbytes (), "iso8859-1 ");
Return ISO;
}
}
Catch (exception E)
{
System. Err. Print ("encoding conversion error:" + E. getmessage ());
Return "";
}
}
}
/*************************************** ********/
// The following is the calling program
Jbfilesave. downloadattachment (filepath + excelfilename, excelfilename, httpservletresponse );
In this way, the dialog box will pop up when executing the above code, and you can choose to save the path, file name or something.