Java content disposition attachment

Spring HttpHeaders CONTENT_DISPOSITION

Spring HttpHeaders CONTENT_DISPOSITION The HTTP Content-Disposition header field name.

Syntax

The field CONTENT_DISPOSITION() from HttpHeaders is declared as:

public static final String CONTENT_DISPOSITION = "Content-Disposition"; 

Example

The following code shows how to use Spring HttpHeaders.CONTENT_DISPOSITION

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; import org.springframework.util.StreamUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartResolver; import org.springframework.web.multipart.support.StandardServletMultipartResolver; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.InputStream; import java.nio.file.Paths; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @Controller/* w w w . d e mo 2 s. c o m*/ @SpringBootApplication public class ZipDemoApplication < public static void main(String[] args) < SpringApplication.run(ZipDemoApplication.class, args); > @Bean public MultipartResolver multipartResolver() < return new StandardServletMultipartResolver(); > @RequestMapping(path = "/files") public void zipDownload(@RequestParam List files, HttpServletResponse response) throws IOException < response.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE); response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=download.zip"); try (ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream())) < for (MultipartFile file : files) < String fileName = Paths.get(file.getOriginalFilename()).getFileName().toString(); try (InputStream input = file.getInputStream()) < zipOutputStream.putNextEntry(new ZipEntry(fileName)); StreamUtils.copy(input, zipOutputStream); // write per 4KB > > > > >
import com.xbcai.model.User; import com.xbcai.result.Result; import com.xbcai.service.file.StorageFileNotFoundException; import com.xbcai.service.file.StorageService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.Resource; import org.springframework.http.HttpHeaders; import org.springframework.http.ResponseEntity; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder; import java.util.stream.Collectors; @RestController/* w w w . d e m o 2 s .c om */ public class FileUploadController < @Autowired private StorageService storageService; @GetMapping("/tt") public Result tt() < User t = new User(); t.setAddress("gz"); t.setName("xbcai"); t.setSex("nv"); return Result.success(t); > @GetMapping("/listUploadedFiles") public ResponseEntity listUploadedFiles() < return ResponseEntity.ok(storageService.loadAll() .map(path -> MvcUriComponentsBuilder .fromMethodName(FileUploadController.class, "serveFile", path.getFileName().toString()) .build().toString()) .collect(Collectors.toList())); > @GetMapping("/files/") @ResponseBody public ResponseEntityResource> serveFile(@PathVariable String filename) < Resource file = storageService.loadAsResource(filename); return ResponseEntity.ok() .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getFilename() + "\"") .body(file); > @PostMapping("/handleFileUpload") public ResponseEntityString> handleFileUpload(@RequestParam("file") MultipartFile file) throws Exception < storageService.store(file); return ResponseEntity.ok("You successfully uploaded " + file.getOriginalFilename() + "!"); > @ExceptionHandler(StorageFileNotFoundException.class) public ResponseEntity handleStorageFileNotFound(StorageFileNotFoundException exc) < return ResponseEntity.notFound().build(); > public static void main(String[] args) < System.out.println(StringUtils.getFilename("mypath/myfile.txt")); System.out.println(StringUtils.getFilename("D:\\private/______/__?_________/Joanna.png")); System.out.println(StringUtils.cleanPath("D://private/______/__?_________/Joanna.png")); System.out.println(StringUtils.cleanPath("d:/java/wolfcode/../../other/Some.java")); > >
import com.fast.cloud.domain.ComRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.ComponentScan; import org.springframework.core.env.Environment; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ZeroCopyHttpOutputMessage; import org.springframework.http.codec.multipart.FilePart; import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.server.ServerResponse; import reactor.core.publisher.Mono; import java.io.File; import java.io.IOException; @SpringBootApplication/* w w w. d em o 2 s. c o m*/ @EnableEurekaClient @Controller @ComponentScan("com.fast.cloud") public class ServiceHiReactApplication extends BaseController < @Autowired Environment environment; public static void main(String[] args) < SpringApplication.run(ServiceHiReactApplication.class, args); > @RequestMapping("/hello/") @ResponseBody public MonoString> sayHiFromClientOne(@PathVariable(name = "name") String name) < return Mono.just("hello:" + name + "port:" + environment.getProperty("server.port")); > @RequestMapping("/hello2") @ResponseBody public MonoString> hello2(@RequestParam("sort") String sortField, @RequestParam("direction") String sortDirection) < return Mono.just("sortField:" + sortField + "sortDirection:" + sortDirection); > @RequestMapping("/hello3") @ResponseBody public MonoString> hello3(@RequestBody ComRequest comRequest) < return Mono.just("comRequest.getName():" + comRequest.getName()); > @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) @ResponseBody MonoString> upload(@RequestPart(value = "file") FilePart file) < return Mono.just(file.filename()); > @GetMapping(value = "/download") MonoVoid> download(ServerHttpResponse response) throws IOException < ZeroCopyHttpOutputMessage zeroCopyResponse = (ZeroCopyHttpOutputMessage) response; response.getHeaders().set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=parallel.png"); response.getHeaders().setContentType(MediaType.IMAGE_PNG); Resource resource = new ClassPathResource("images.jpg"); File file = resource.getFile(); return zeroCopyResponse.writeWith(file, 0, file.length()); > // @GetMapping(value = "/download2") // @ResponseBody // Mono download2(ServerHttpResponse response) throws IOException //// ZeroCopyHttpOutputMessage zeroCopyResponse = (ZeroCopyHttpOutputMessage) response; //// response.getHeaders().set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=parallel.png"); //// response.getHeaders().setContentType(MediaType.IMAGE_PNG); //// //// Resource resource = new ClassPathResource("images.jpg"); //// File file = resource.getFile(); //// return zeroCopyResponse.writeWith(file, 0, file.length()); // Resource resource = new ClassPathResource("images.jpg"); // File file = resource.getFile(); // return ServerResponse.ok() // .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=parallel.png") // .contentType(MediaType.IMAGE_PNG) // .body(BodyInserters.fromResource(resource)) // .switchIfEmpty(Mono.empty()); // > // public Mono test(ServerRequest request) throws Exception // File excel = new File("tmp"); // var out = new FileOutputStream(excel); // var writer = new ExcelWriter(out, ExcelTypeEnum.XLSX,false); // Sheet sheet1 = new Sheet(1, 0); // writer.write(Arrays.asList(), sheet1); // writer.finish(); // return ServerResponse.ok() // .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=test.xlsx") // .contentType(new MediaType("multipart/form-data")) // .body((p, a) -> // var resp = (ZeroCopyHttpOutputMessage) p; // return resp.writeWith(excel, 0, excel.length()); // >).doFinally(a -> ); // > >

  • Spring HttpHeaders getDate()
  • Spring HttpHeaders setContentLength(long contentLength)
  • Spring HttpHeaders setContentDispositionFormData(String name, @Nullable String filename)
  • Spring HttpHeaders CONTENT_DISPOSITION
  • Spring HttpHeaders getContentType()
  • Spring HttpHeaders ACCEPT
  • Spring HttpHeaders getAllow()

demo2s.com | Email: | Demo Source and Support. All rights reserved.

Источник

Читайте также:  Python binary images with
Оцените статью