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 -> ); // > >
Related
- 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.
What is content disposition in java
This class represents a MIME ContentDisposition value. It provides methods to parse a ContentDisposition string into individual components and to generate a MIME style ContentDisposition string.
Constructor Summary | |
---|---|
ContentDisposition () No-arg Constructor. | |
ContentDisposition (java.lang.String s) Constructor that takes a ContentDisposition string. | |
ContentDisposition (java.lang.String disposition, ParameterList list) Constructor. |
Method Summary | |
---|---|
java.lang.String | getDisposition () Return the disposition value. |
java.lang.String | getParameter (java.lang.String name) Return the specified parameter value. |
ParameterList | getParameterList () Return a ParameterList object that holds all the available parameters. |
void | setDisposition (java.lang.String disposition) Set the disposition. |
void | setParameter (java.lang.String name, java.lang.String value) Set the specified parameter. |
void | setParameterList (ParameterList list) Set a new ParameterList. |
java.lang.String | toString () Retrieve a RFC2045 style string representation of this ContentDisposition. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
ContentDisposition
public ContentDisposition()
ContentDisposition
Parameters: disposition — disposition list — ParameterList Since: JavaMail 1.2
ContentDisposition
Constructor that takes a ContentDisposition string. The String is parsed into its constituents: dispostion and parameters. A ParseException is thrown if the parse fails.
Parameters: s — the ContentDisposition string. Throws: ParseException — if the parse fails. Since: JavaMail 1.2
Method Detail |
---|
getDisposition
public java.lang.String getDisposition()
Returns: the disposition Since: JavaMail 1.2
getParameter
public java.lang.String getParameter(java.lang.String name)
Returns: parameter value Since: JavaMail 1.2
getParameterList
Return a ParameterList object that holds all the available parameters. Returns null if no parameters are available.
Returns: ParameterList Since: JavaMail 1.2
setDisposition
public void setDisposition(java.lang.String disposition)
Parameters: disposition — the disposition Since: JavaMail 1.2
setParameter
public void setParameter(java.lang.String name, java.lang.String value)
Parameters: name — parameter name value — parameter value Since: JavaMail 1.2
setParameterList
Parameters: list — ParameterList Since: JavaMail 1.2
toString
public java.lang.String toString()
Retrieve a RFC2045 style string representation of this ContentDisposition. Returns null if the conversion failed.
Overrides: toString in class java.lang.Object Returns: RFC2045 style string Since: JavaMail 1.2
| ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Copyright © 2009-2011, Oracle Corporation and/or its affiliates. All Rights Reserved. Use is subject to license terms.
Generated on 10-February-2011 12:41