A program that pops up the save file dialog box in B/S

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.