Last active
April 16, 2018 17:47
-
-
Save xandreafonso/73e585d7ba0c54ae219ada5e8405f272 to your computer and use it in GitHub Desktop.
Exemplo de controlador que faz upload e download de arquivos com Spring MVC.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.projeto.model.Arquivo; | |
import java.io.IOException; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.http.HttpHeaders; | |
import org.springframework.http.HttpStatus; | |
import org.springframework.http.MediaType; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.web.bind.annotation.GetMapping; | |
import org.springframework.web.bind.annotation.PostMapping; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestParam; | |
import org.springframework.web.bind.annotation.ResponseStatus; | |
import org.springframework.web.bind.annotation.RestController; | |
import org.springframework.web.multipart.MultipartFile; | |
@RestController | |
@RequestMapping("/arquivos") | |
public class DownUpController { | |
@PostMapping("/upload") | |
public Arquivo upload(@RequestParam MultipartFile arquivo) throws IOException { | |
// Salvar o arquivo e retornar o objeto Arquivo. | |
return null; | |
} | |
@GetMapping("/download") | |
public ResponseEntity<byte[]> download(@RequestParam String nome) throws Exception { | |
Arquivo arquivo = null; // Ler arquivo do banco a partir parâmetro (que é "nome", no caso). | |
byte[] bytes = null; // Ler bytes do arquivo. Os bytes podem estar tanto no banco quanto no disco. | |
return ResponseEntity.ok() | |
.header(HttpHeaders.CONTENT_TYPE, arquivo.getContentType()) | |
.body(bytes); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment