spring mvc 接收前端传入的数组对象

最近弄了一个多图片上传,结果一直出问题,后来发现是接收的数据没有处理好,前端传过来的是一个数组对象,对象里面有一个title的属性,用来放置图片的base64数据,而且数据还包含了data头部,因为spring mvc 不能直接接收数组 和集合,所以,就需要我们进行封装

我现在的做法是,先创建一个实体类,放title属性

public class ImageInfo {
    
    private String title;

    public String  getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

然后再创建一个集合类

public class ImageList {
    private List images;

    public List getImages() {
        return images;
    }

    public void setImages(List images) {
        this.images = images;
    }

}

这样在Controller里就可以直接使用这个对象接收前端传入的数组对象了

  

 @RequestMapping("/perfectInfo")
    @ResponseBody
    public BaseResult perfectInfo(TbrhInfo tbrhInfo ,ImageList  imageList,@RequestParam(value = "xslFile")MultipartFile xslFile) throws BaseAppException{
        BaseResult result  = tbrhInfoTxService. perfectInfo(tbrhInfo, imageList, xslFile);
        return result ;
    }

 

前端是使用formData提交方式

spring mvc 接收前端传入的数组对象_第1张图片

图片上传还是上传到ftp服务器中,至于具体方法,上篇博客中有写到,这里只贴出调用过程

public  void uploadImage(String brhId,ImageList images,String fileName){
        String remotePath = ConfigConst.FTP_REMOTEPATH + BaseConst.WEBNAME+ "/" + BaseConst.BRHINFO + "/" + brhId + "/"+DictConst.CREDENTIALS+"/";
        String name = null; 
        InputStream inputStream = null;
        for (int i = 0; i < images.getImages().size(); i++) {
                name = fileName+i+".jpg";
                try {
                    String fileDesc = images.getImages().get(i).getTitle().split(",")[1];
                    // 解密
                    byte[] b = Base64.decodeBase64(fileDesc);
                    // 处理数据
                    for (int j = 0; j < b.length; ++j) {
                        if (b[j] < 0) {
                            b[j] += 256;
                        }
                    }
                    
                    inputStream = new ByteArrayInputStream(b);
                    boolean uploadResult = JschFtpUtil.put(ConfigConst.INNER_FTP_REMOTEHOST,ConfigConst.INNER_FTP_PORT, remotePath,
                                    ConfigConst.INNER_FTP_USER,ConfigConst.INNER_FTP_PASSWORD,inputStream, name);
                    if (!uploadResult) {
                        throw new BaseTxException("upload_fail", "文件上传失败,请重新提交");
                    }
                }  finally{
                    try {
                        if (inputStream != null) {
                            inputStream.close();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                }
            }
        }        
                saveAttachment(brhId, remotePath, name, DictConst.CREDENTIALS);
    }

虽然都是小问题,但是困扰了自己这么久,所以还是值得记录一下的,以免下次还是不知道

你可能感兴趣的:(java)