Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package br.edu.unidavi.restapp; import com.google.common.collect.Lists; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; import javax.servlet.annotation.MultipartConfig; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.hateoas.config.EnableHypermediaSupport; import org.springframework.hateoas.mvc.ControllerLinkBuilder; import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; import org.springframework.http.HttpEntity; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; /** * * @author Leo */ @Controller @RestController @RequestMapping("/listFileUpload") @MultipartConfig(fileSizeThreshold = 1024 * 1024 * 10, // 10 MB maxFileSize = 1024 * 1024 * 50, // 50 MB maxRequestSize = 1024 * 1024 * 100) // 100 MB @EnableHypermediaSupport(type = { EnableHypermediaSupport.HypermediaType.HAL }) public class FileUploadController { @Autowired FileUploadRepository fileUploadRepository; @RequestMapping(method = RequestMethod.GET) public HttpEntity<List<FileUpload>> getFileUpload() { ArrayList<FileUpload> files = Lists.newArrayList(fileUploadRepository.findAll()); for (FileUpload fileUpload : files) { fileUpload.add(linkTo(ControllerLinkBuilder.methodOn(FileUploadController.class).getFileUpload()) .withSelfRel()); } return new ResponseEntity<List<FileUpload>>(files, HttpStatus.OK); } @RequestMapping(value = "/cadastro", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) public HttpEntity<FileUpload> setFileUpload(@RequestBody FileUpload fileUpload) { fileUploadRepository.save(fileUpload); return new ResponseEntity<FileUpload>(fileUpload, HttpStatus.OK); } @RequestMapping(value = "/upload", method = RequestMethod.POST) public @ResponseBody String handleFileUpload(@RequestParam("file") MultipartFile file) { if (!file.isEmpty()) { try { String name = file.getOriginalFilename(); System.out.println(name); byte[] bytes = file.getBytes(); BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(name))); stream.write(bytes); stream.close(); return "You successfully uploaded !"; } catch (Exception e) { return "You failed to upload => " + e.getMessage(); } } else { return "You failed to upload because the file was empty."; } } }