JavaWeb文件的上传及下载

x33g5p2x  于2021-12-06 转载在 Java  
字(2.8k)|赞(0)|评价(0)|浏览(271)

下面是平时写网站时常用的一种文件下载的方法和一个读取zip压缩包文件的代码
见代码:

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class IOTest extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("templates/SimpleTemplate.xlsx");
// InputStream inputStream = new ClassPathResource("/templates/SimpleTemplate.xlsx").getInputStream();
        String returnName = URLEncoder.encode("SimpleTemplate.xlsx","UTF-8");
        try {
            if (inputStream != null) {
                download(inputStream, resp, returnName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /** * 文件下载 * @param inputStream 将文件读入输入流中 * @param response HttpServletResponse * @param returnName 返回的文件名 * @throws Exception 异常抛出 */
    public void download(InputStream inputStream, HttpServletResponse response, String returnName) throws Exception {
        response.setContentType("application/octet-stream;charset=utf-8");
        returnName = response.encodeURL(new String(returnName.getBytes(), StandardCharsets.UTF_8));			//保存的文件名,必须和页面编码一致,否则乱码
        response.addHeader("Content-Disposition",   "attachment;filename=" + returnName);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte [] buffer  = new byte[1024];
        int len = 0;
        while((len = inputStream.read(buffer)) != -1) {
            baos.write(buffer, 0,  len);
        }
        response.setHeader("Content-Length", String.valueOf(baos.size()));
        BufferedOutputStream bos = null;
        bos = new BufferedOutputStream(response.getOutputStream());
        baos.writeTo(bos);
        bos.close();
        baos.close();
    }

    /** * 读取zip压塑包文件内容 * @param srcZip 压缩文件夹 * @param fileName 读取文件名称 */
    public void readZipFile(String srcZip,String fileName) {
        FileInputStream fileInputStream = null;
        ZipInputStream zipInputStream = null;
        try {
            //获取文件输入流
// fileInputStream = new FileInputStream("D:\\aa\\dt95.zip");
            fileInputStream = new FileInputStream(srcZip);
            //获取ZIP输入流
            zipInputStream = new ZipInputStream(new BufferedInputStream(fileInputStream),Charset.forName("GBK"));
            //定义ZipEntry 置为null,避免由于重复调用
            ZipEntry ze = null;
            //循环遍历
            while ((ze = zipInputStream.getNextEntry()) != null){
                System.out.println("文件名:"+ze.getName()+" 文件大小:"+ze.getSize()+" bytes");
                System.out.println("文件内容:");
                if (ze.getName().equals(fileName)) {
                    //读取
                    BufferedReader br = new BufferedReader(new InputStreamReader(zipInputStream, Charset.forName("GBK")));

                    String line;
                    //内容不为空,输出
                    while ((line = br.readLine()) != null) {
                        System.out.println(line);
                    }
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭流
            try {
                if (zipInputStream != null){
                    zipInputStream.close();
                }
                if (fileInputStream != null){
                    fileInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

相关文章

微信公众号

最新文章

更多