When developing B / S mode, encounter such a problem, the client needs to perform a program to download the relevant information submitted by the client to the local. My idea is like this, first generate a file on the server, then download it to the client, delete. The function is achieved, but I always feel a little bare, but I don't know what is good way to solve, the following is my program: package chartreport;
Imports.Apache.struts.Avax.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); // hard disk is converted into an actual address InputStream input = null; OutputStream output = null; File f = null; try {{input = new FileInputStream (fileOnDisk) try; f = new File (fileOnDisk);} catch (IOException e) {// System.out.println ("Can not get attchment on disk");
BYTE [] buffer = getbook; 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 (); output.clush (); output.close (); output = null;} catch (ooException {//System.out.println ("error Download Attachment.");} Finally {if (input! = Null) {Try {INPUT.CLOSE ();} catch (ioException ex) {}}}}} NULL) {Try {Output.close ();} catch (ooexception ex) {}}}}
/ ** * * @param inputStream InputStream * @throws IOException * @return byte [] * / public byte [] getBytes (InputStream inputStream) throws IOException {ByteArrayOutputStream 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 change {string result = null; byte temp []; try {temp = str.getbytes ("GB2312"); Result = new string Temp);} catch (java.io.unsupportedEncodingexception ex) { //System.out.println (ex., ();} 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 "Coded conversion error:" E.getMessage ()); return "";}}} / ****************************** ****************** //// below is the call program JBFileSave.DownloadAttachment (FilePath ExcelfileName, ExcelfileName, httpservletResponse); this will pop up dialog box when executing the above code ,