已针对ServletOutputStream的此响应调用getOutputStream()。

时间:2018-10-29 14:00:38

标签: java jsp

使用

时已为此响应调用了getOutputStream()作为异常。
ServletOutputStream outputStream = response.getOutputStream(); 

在我的代码中。 这是示例代码。首先,我将输入文件放置在本地路径中,该路径为svg图像。之后,svg将转换为png并从浏览器下载。

{ //code to write image into local path
                    File filesvg = new File(inFile); 
                    svg  = svg.replace("1e-006","0.000001");
                    FileWriter writer = new FileWriter(filesvg);
                    writer.write(svg);
                    writer.close();
                    //here some lines of code to convert to png
                    return outFile;
                    }
                    //Code to download image from browser.
                    response.setContentType("application/octet-stream");
                        response.setHeader("Content-Disposition", 
                       "attachment;filename=\"" + filename + ext + "\"");
                        FileInputStream fileIn = new FileInputStream(file);
                        ServletOutputStream outputStream = 
                        response.getOutputStream();
                        byte[] outputByte = new byte[4096];
                        int byteRead;
                        while ((byteRead = fileIn.read(outputByte, 0, 4096)) 
                       != -1) {
                            outputStream.write(outputByte, 0, byteRead);
                        }
                        fileIn.close();
                        outputStream.flush();
                        outputStream.close(); 

0 个答案:

没有答案