SpingBoot上传文字与文件

接收安卓端通过retrofit上传的文字及图片,安卓代码见这里
直接上代码。

import com.shufeng.communitymanager.base.bean.RespEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;

@RestController
public class RestController
{
    @ResponseBody
    @RequestMapping(value = "/shop/add", method = RequestMethod.POST)
    public RespEntity add(@RequestParam Map params, @RequestParam("multipartFiles")List files)
//    public RespEntity add(HttpServletRequest request)
    {
        String _Type = params.get("type");
        System.out.println("_Type-->" + _Type);
        
        String fileName = null;
        String msg = "";
        if (files != null && files.size() > 0)
        {
            for (int i = 0; i < files.size(); i++)
            {
                try
                {
                    fileName = files.get(i).getOriginalFilename();
                    byte[] bytes = files.get(i).getBytes();
                    BufferedOutputStream buffStream =
                            new BufferedOutputStream(new FileOutputStream(new File("/tmp/" + fileName)));
                    buffStream.write(bytes);
                    buffStream.close();
                    msg += "You have successfully uploaded " + fileName;
                } catch (Exception e)
                {
                    msg = "You failed to upload " + fileName + ": " + e.getMessage();
                    e.printStackTrace();
                }
            }
        } else
        {
            msg = "Unable to upload. File is empty.";
        }
        System.out.println("--msg--" + msg);
        RespEntity _Result = new RespEntity();
        return _Result;
    }
}

设置文件上传的大小

spring:
  servlet:
    multipart:
      # 上传的单个最大文件
      max-file-size: 1MB

你可能感兴趣的:(SpingBoot上传文字与文件)