Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
themeEmacs
titleupload
linenumberstrue
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
	import org.springframework.stereotype.Controller;
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.multipart.MultipartFile;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

@Controller
public class UploadController {
    static String ROOT_PATH = "C:\\workspace\\synapEditorsample\\out\\artifacts\\synapEditorsample_war_exploded";
	    static String IMAGE_UPLOAD_DIR = "uploads";

    @RequestMapping(value = "/")
    public String test(){
        return "index";
    }

    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
	
        String ext = fileName.substring(fileName.lastIndexOf('.'));
	
        String saveFileName = UUID.randomUUID().toString() + ext;

 	       String uploadPath = ROOT_PATH + File.separator + IMAGE_UPLOAD_DIR;
	        File uploadDir = new File(uploadPath);
	if
        if (!uploadDir.exists()) {
	            uploadDir.mkdirs();
	}

	
        }

        byte[] bytes = file.getBytes();
	        Path path = Paths.get(uploadPath + File.separator + saveFileName);
	
        Files.write(path, bytes);

      	  Map<String, Object> map = new HashMap<>();

	        // 브라우저에서 접근가능한 경로를 uploadPath에 담아서 넘겨줍니다.
	
        map.put("uploadPath", "uploads/" + saveFileName);

        return map;
    }
}

2. Importing MS Word / LibreOffice documents

...