1. 程式人生 > >java檔案下載,中文不顯示

java檔案下載,中文不顯示

@RequestMapping("/downLoadZipFile")
    public void downLoadZipFile(String videoAddress, String videoName, HttpServletResponse response) throws Exception {


        String filePath = PlatformConstants.IMG_CONTEXT_PATH + "/" + videoAddress;
        URL url = new URL(filePath);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        DataInputStream is = new DataInputStream(connection.getInputStream());


        String fileName = filePath.substring(filePath.lastIndexOf("/"));
        String suffix = fileName.substring(fileName.lastIndexOf("."), fileName.length());
        videoName = new String(videoName.getBytes(), "ISO-8859-1");
        response.reset(); // 必要地清除response中的快取資訊
        response.setHeader("Content-Disposition", "attachment; filename=" + videoName + suffix);
        response.setContentType("application/octet-stream");// 根據個人需要,這個是下載檔案的型別
        javax.servlet.ServletOutputStream out = response.getOutputStream();


        byte[] content = new byte[1024];
        int length = 0;
        while ((length = is.read(content)) != -1) {
            out.write(content, 0, length);
        }
        out.write(content);
        out.flush();
        out.close();


    }