java 网页请求,下载pdf文件

    技术2026-02-15  20

    package com.zyy.springmvc.controller; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import com.zyy.springmvc.model.ResultVo; @Controller @RequestMapping("file") public class FileUploadController { @RequestMapping(value = "/pdf", method = RequestMethod.GET) public void upload(HttpServletResponse response) throws IOException { byte[] buf = getBytes(); // 将byte数组写到response里面 response.getOutputStream().write(buf, 0, buf.length); } private byte[] getBytes() throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try (FileInputStream in = new FileInputStream(new File("C:\\Users\\wild\\Desktop\\pdf\\aaa.pdf"))) { int len; byte[] b = new byte[1024]; while ((len=in.read(b)) != -1) { bos.write(b, 0, len); } } return bos.toByteArray(); } }

     

    Processed: 0.009, SQL: 9