web浏览器访问系统接口弹出下载框下载文件

1.下载文件

        @ApiOperation("下载文件")
        @GetMapping("/download/{id}/byBi")
        public void downFile(@ApiParam(required = true,value = "id")@PathVariable(value = "id") String id, HttpServletRequest request, HttpServletResponse response) {
                EntityWrapper<ProjectExamineAttachment> wrapper = new EntityWrapper<>();
                wrapper.eq("file_id", id);
                response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
                response.setCharacterEncoding("UTF-8");
                List<ProjectExamineAttachment> result = this.tmProjectExamineAttachmentService.queryList(wrapper);
                if(!CollectionUtils.isEmpty(result)){
                        try {
                                InputStream inputStream = docRdpService.downloadAttachment(id);
                                OutputStream outputStream = response.getOutputStream();
                                response.setHeader("Content-Disposition", "attachment; filename="+URLEncoder.encode(result.get(0).getName(),"UTF-8"));
                                byte[] buffer = new byte[4096];
                                int bytesRead;
                                while ((bytesRead = inputStream.read(buffer)) != -1) {
                                        outputStream.write(buffer, 0, bytesRead);
                                }
                                System.out.println("文件下载完成");

                                inputStream.close();
                                outputStream.close();
                        } catch (IOException e) {
                                e.printStackTrace();
                        }
                }
        }

2.下载zip

	@ApiOperation("下载zip")
	@GetMapping("/download/{id}/byRfi")
	public void downZip(@ApiParam(required = true,value = "id")@PathVariable(value = "id") String id,
											   HttpServletRequest request, HttpServletResponse response) throws IOException {

		EntityWrapper<ProjectExamineAttachment> wrapper = new EntityWrapper<>();
		wrapper.eq("relate_id", id);

		response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
		response.setHeader("Content-Disposition", "attachment; filename=\"IntakeTable.zip\"");

		ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream());

		try {
			List<ProjectExamineAttachment> result = this.tmProjectExamineAttachmentService.queryList(wrapper);
			for (ProjectExamineAttachment item : result) {
				InputStream inputStream = docRdpService.downloadAttachment(item.getFileId());
				addToZip(item.getName(), inputStream, zipOut);
			}
			zipOut.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void addToZip(String fileName, InputStream inputStream, ZipOutputStream zipOut) throws IOException {
		zipOut.putNextEntry(new ZipEntry(fileName));
		StreamUtils.copy(inputStream, zipOut);
		zipOut.closeEntry();
		inputStream.close();
	}

3.封装MultipartFile

	/**
	 * 获取封装得MultipartFile
	 *
	 * @param inputStream inputStream
	 * @param fileName    fileName
	 * @return MultipartFile
	 */
	public MultipartFile getMultipartFile(InputStream inputStream, String fileName) {
		FileItem fileItem = createFileItem(inputStream, fileName);
		//CommonsMultipartFile是feign对multipartFile的封装,但是要FileItem类对象
		return new CommonsMultipartFile(fileItem);
	}

4.FileItem类对象创建

	/**
	 * FileItem类对象创建
	 *
	 * @param inputStream inputStream
	 * @param fileName    fileName
	 * @return FileItem
	 */
	public FileItem createFileItem(InputStream inputStream, String fileName) {
		FileItemFactory factory = new DiskFileItemFactory(16, null);
		String textFieldName = "file";
		FileItem item = factory.createItem(textFieldName, MediaType.MULTIPART_FORM_DATA_VALUE, true, fileName);
		int bytesRead = 0;
		byte[] buffer = new byte[10 * 1024 * 1024];
		OutputStream os = null;
		//使用输出流输出输入流的字节
		try {
			os = item.getOutputStream();
			while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
				os.write(buffer, 0, bytesRead);
			}
			inputStream.close();
		} catch (IOException e) {
			log.error("Stream copy exception", e);
			throw new IllegalArgumentException("文件上传失败");
		} finally {
			if (os != null) {
				try {
					os.close();
				} catch (IOException e) {
					log.error("Stream close exception", e);

				}
			}
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					log.error("Stream close exception", e);
				}
			}
		}

		return item;
	}
posted @ 2023-08-22 15:14  谜一样的心Max  阅读(131)  评论(0)    收藏  举报