|
| 1 | +package com.baeldung.sampleapp.web.controller; |
| 2 | + |
| 3 | +import java.io.BufferedOutputStream; |
| 4 | +import java.io.File; |
| 5 | +import java.io.FileOutputStream; |
| 6 | +import java.text.DateFormat; |
| 7 | +import java.text.SimpleDateFormat; |
| 8 | +import java.util.Date; |
| 9 | + |
| 10 | +import org.springframework.web.bind.annotation.RequestBody; |
| 11 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 12 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 13 | +import org.springframework.web.bind.annotation.RequestParam; |
| 14 | +import org.springframework.web.bind.annotation.RestController; |
| 15 | +import org.springframework.web.multipart.MultipartFile; |
| 16 | + |
| 17 | +import com.baeldung.sampleapp.web.dto.Foo; |
| 18 | + |
| 19 | +// used to test HttpClientPostingTest |
| 20 | +@RestController |
| 21 | +public class SimplePostController { |
| 22 | + |
| 23 | + @RequestMapping(value = "/users", method = RequestMethod.POST) |
| 24 | + public String postUser(@RequestParam final String username, @RequestParam final String password) { |
| 25 | + return "Success" + username; |
| 26 | + } |
| 27 | + |
| 28 | + @RequestMapping(value = "/users/detail", method = RequestMethod.POST) |
| 29 | + public String postUserDetail(@RequestBody final Foo entity) { |
| 30 | + return "Success" + entity.getId(); |
| 31 | + } |
| 32 | + |
| 33 | + @RequestMapping(value = "/users/multipart", method = RequestMethod.POST) |
| 34 | + public String uploadFile(@RequestParam final String username, @RequestParam final String password, @RequestParam("file") final MultipartFile file) { |
| 35 | + if (!file.isEmpty()) { |
| 36 | + try { |
| 37 | + final DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HH.mm.ss"); |
| 38 | + final String fileName = dateFormat.format(new Date()); |
| 39 | + final File fileServer = new File(fileName); |
| 40 | + fileServer.createNewFile(); |
| 41 | + final byte[] bytes = file.getBytes(); |
| 42 | + final BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(fileServer)); |
| 43 | + stream.write(bytes); |
| 44 | + stream.close(); |
| 45 | + return "You successfully uploaded " + username; |
| 46 | + } catch (final Exception e) { |
| 47 | + return "You failed to upload " + e.getMessage(); |
| 48 | + } |
| 49 | + } else { |
| 50 | + return "You failed to upload because the file was empty."; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @RequestMapping(value = "/users/upload", method = RequestMethod.POST) |
| 55 | + public String postMultipart(@RequestParam("file") final MultipartFile file) { |
| 56 | + if (!file.isEmpty()) { |
| 57 | + try { |
| 58 | + final DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HH.mm.ss"); |
| 59 | + final String fileName = dateFormat.format(new Date()); |
| 60 | + final File fileServer = new File(fileName); |
| 61 | + fileServer.createNewFile(); |
| 62 | + final byte[] bytes = file.getBytes(); |
| 63 | + final BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(fileServer)); |
| 64 | + stream.write(bytes); |
| 65 | + stream.close(); |
| 66 | + return "You successfully uploaded "; |
| 67 | + } catch (final Exception e) { |
| 68 | + return "You failed to upload " + e.getMessage(); |
| 69 | + } |
| 70 | + } else { |
| 71 | + return "You failed to upload because the file was empty."; |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments