最近搜索

一次接受多个图片。处理 多文件上传。多图片上传。 小程序和app都这样接受图片。

浏览:379
管理员 2022-10-13 15:53

如果 一次接受多个图片这样设计   android  app的 上传图片接口就是用的这个。 一次可以选择9个图片。

/**
     * 文件 上传,
     * /api/uploadimage
     * @param file
     *  //自定义上传图片的名字为userId.jpg
            //String fileName = request.getAttribute("userId") + "123456.jpg";
     */
    @ResponseBody
    @RequestMapping("/api/uploadimage")
    public JSONObject uploadimage(HttpServletRequest request)throws Exception {
        JSONObject result = new  JSONObject();
        //获取文件需要上传到的路径
        String webPath=request.getServletContext().getRealPath("");
        String filePath= "/static/xcx_upload/image/"+DateUtil.formatDate(new Date(), "yyyyMMdd")+"/";
        FileUtil.makeDirs(webPath+filePath);
         
        request.setCharacterEncoding("utf-8"); //设置编码
        try {
            StandardMultipartHttpServletRequest req = (StandardMultipartHttpServletRequest) request;
            Iterator<String> iterator = req.getFileNames();
            //String fileName = (String) req.getAttribute("bianhao") ;
             
            while (iterator.hasNext()) {
                //拿到所有图片
                List<MultipartFile> filelist = req.getFiles(iterator.next());
                for(MultipartFile file :filelist ) {
                     // 获取文件名
                     String fileNames = file.getOriginalFilename();
                     int split = fileNames.lastIndexOf(".");
                     //获取上传文件的后缀
                     String extName = fileNames.substring(split + 1, fileNames.length());
                     //申明UUID
                     String uuid = UUID.randomUUID().toString().replace("-", "");
                     //组成新的图片名称
                     String newName = uuid + "." + extName;
                     System.out.println(newName);
                     String destPath = webPath+filePath + newName;
                     //真正写到磁盘上
                     File file1 = new File(destPath);
                     OutputStream out = new FileOutputStream(file1);
                     out.write(file.getBytes());
                     out.close();
                }
            }
        } catch (Exception e) {
        }
         
        
         
        return result;
    }





还有一种接受的方法是


public List<String> receiveFileToDir(List<MultipartFile> multipartFiles) {
        // 输出的文件流保存到本地文件
        // uploadDir 是设置好的文件保存地址,可自行设置
        String path = uploadDir;
        // 判断文件保存目录是否存在,不存在则创建相关目录
        File tempFile = new File(path);
        if (!tempFile.exists()) {
            tempFile.mkdirs();
        }
        List<String> fileNameList = new ArrayList<>();
        for (MultipartFile file : multipartFiles){
            OutputStream os = null;
            InputStream inputStream = null;
            String originalFilename = file.getOriginalFilename();
            String[] split = originalFilename.split("\\.");

            String fileName = codeUtilBuil() + "." + split[1];
            try {
                inputStream = file.getInputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
		// File.separato 系统默认分隔符,相当于 / 
            try {
                os = new FileOutputStream(tempFile.getPath()+ "/" + File.separator + fileName);
                // 开始读取
                byte[] bs = new byte[1024];
                // 读取数据
                int length;
                while ((length = inputStream.read(bs)) != -1) {
                    os.write(bs, 0, length);
                }
            } catch (Exception e) {
 e.printStackTrace();
            } finally {
                // 关闭所有链接
                try {
                    os.close();
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            fileNameList.add(path + "/" +fileName);
        }
        return fileNameList;
    }


联系站长

站长微信:xiaomao0055

站长QQ:14496453