package tk.erdmko.conrollers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.FileOutputStream;

import javax.servlet.http.HttpServletRequest;

import tk.erdmko.models.FailResponse;
import tk.erdmko.models.OkResponse;
import tk.erdmko.models.SimpleJsonResponse;

@Controller
public class FileUploadController {

    @Value("${file.upload.directory}")
    private String fileUploadDirectory;

    @Autowired
    private HttpServletRequest request;

    @RequestMapping(value="/upload", method=RequestMethod.POST)
    public @ResponseBody
    SimpleJsonResponse handleFileUpload(@RequestParam("name") String name,
                            @RequestParam("file") MultipartFile file){
        if (!file.isEmpty()) {
            try {
                String originalFileExtension = file
                        .getOriginalFilename()
                        .substring(file.getOriginalFilename().lastIndexOf("."));
                String newFilename = fileUploadDirectory + "/" + name + originalFileExtension;
                FileOutputStream newFile = new FileOutputStream(newFilename);
                newFile.write(file.getBytes());
                newFile.close();
                return new OkResponse("File downloaded to '"+newFilename+"' path");
            } catch (Exception e) {
                return new FailResponse(name+" => "+e.getMessage());
            }
        } else {
            return new FailResponse("You failed to upload " + name + " because the file was empty.");
        }
    }
}

@@ -146,16 +146,81 @@
 .Value;%0A
+import org.springframework.messaging.simp.SimpMessagingTemplate;%0A
 import o
@@ -261,16 +261,16 @@
 roller;%0A
-
 import o
@@ -894,16 +894,61 @@
 ctory;%0A%0A
+    private SimpMessagingTemplate template;%0A%0A
     @Aut
@@ -995,16 +995,138 @@
 quest;%0A%0A
+    @Autowired%0A    public FileUploadController(SimpMessagingTemplate template) %7B%0A        this.template = template;%0A    %7D%0A%0A
     @Req
@@ -1798,24 +1798,24 @@
 etBytes());%0A
-
             
@@ -1835,16 +1835,85 @@
 lose();%0A
+                this.template.convertAndSend(%22/wsOut%22, newFilename);%0A
         

Send info from mobile to browser