使用spring mvc中的rest服务上传文件

时间:2017-05-07 08:52:12

标签: spring web-services rest spring-mvc server-side

我想使用web服务和spring mvc将文件(任何类型的文件)上传到forlder,所以我有一个服务器端和一个客户端。 在我的客户端,这是代码

    @RequestMapping(value = "/uploadMultipleFile", method = RequestMethod.POST , produces="application/json")
    public @ResponseBody
    Boolean uploadMultipleFileHandler(
            @RequestParam("name") MultipartFile[] files) {
        MailService ms= new MailService();
        Map<String, List<ByteArrayResource>>rval = new HashMap<String, List<ByteArrayResource>>();
        String message = "";
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        List<Object> files1 = new ArrayList<>();
        List<Object> files2 = new ArrayList<>();

        for (int i = 0; i < files.length; i++) {
            MultipartFile file = files[i];

System.out.println(file.getOriginalFilename());

                try {
                    byte[] bytes = file.getBytes();
                    files1.add(new ByteArrayResource(bytes));
                    files2.add(file.getOriginalFilename());

                    //System.out.println(map.toString());
                } catch (IOException e) {
                    e.printStackTrace();
                }



        }
        map.put("files", files1);
        map.put("names", files2);
        System.out.println(map.get("files").toString());
        RestTemplate restTemplate = new RestTemplate();
        String SERVER_URI="http://localhost:8080/BackEndFinalVersion";

        Boolean p=restTemplate.postForObject(SERVER_URI+"/uploadMultipleFile", map, Boolean.class);
        System.out.println(p.toString());


        //message = message + ms.encodeFileToBase64Binary( bytes);
        //rval.put("success",message);

        return true;
    }

,服务器端代码为

@RequestMapping(value = "/uploadMultipleFile", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody Boolean uploadMultipleFileHandler(@RequestParam("files") List<Object> files , @RequestParam("names") List<Object> names) {
    //MailService ms= new MailService();

    //Map<String, Object> rval = new HashMap<String, Object>();
    String message = "";
    System.out.println("looool");
    System.out.println(files);
    System.out.println(names);

    //System.out.println(files.get(0).toString());

    for (int i = 0; i < files.size(); i++) {
        System.out.println(files.get(i).getClass());
        String file =   (String)files.get(i);

        try {
            byte[] bytes = file.getBytes();
            //FileUtils.writeStringToFile(new File("log.txt"), file, Charset.defaultCharset());

            // Creating the directory to store file
            String rootPath = "C:/Users/Wassim/Desktop/uploads";
            File dir = new File(rootPath);
            if (!dir.exists())
                dir.mkdirs();



            File serverFile = new File(dir.getAbsolutePath() + File.separator + ( names.get(i)));
            BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile));
            stream.write(bytes);
            stream.close();
            //message = message + "You successfully uploaded file=" + ( (MultipartFile) files.get(i)).getOriginalFilename() + "<br />";
            //FileUtils.writeByteArrayToFile(new File(dir.getAbsolutePath() + File.separator + files.get(i).getOriginalFilename()), ms.decodeFileToBase64Binary(ms.encodeFileToBase64Binary( bytes)));
            //rval.put("success"+i, message);
            System.out.println("noooo");


        } catch (Exception e) {
            message += "You failed to upload " + " => " + e.getMessage();
            //rval.put("error", message);
            return false;
        }
    }
    return true;

我的问题是此代码不仅适用于.txt文件

任何人都可以支持我吗?

0 个答案:

没有答案