uniapp多文件上传

1.前端代码:

uploadFilePromise(arr) {
				let files=arr.map((value,index)=>{
				 return{
				 name:value.name,
				 uri:value.path
				 }
				 });
				return new Promise((resolve, reject) => {
					let uploadTask  = uni.uploadFile({
						url: url_config+'/app/common/v_uploadattachfile.do', 
						files:files,
						// filePath: url,
						// name: 'file',
						// formData: {
						// 	id: 'test'
						// },
						success: (res) => {
							console.log("res",res)
							setTimeout(() => {
								resolve(JSON.parse(res.data))
							}, 1000)
						},
						fail:(e) => {
							uni.$u.toast("上传失败,请重新上传")
						}
					});
					
				})
			}

2.后端代码:

  @RequestMapping(value = "/v_uploadfile.do", method = RequestMethod.POST)
    public ReturnMsg uploadFile(HttpServletRequest request) throws Exception {
        String content = "content-type";
        ReturnMsg msgEntity = new ReturnMsg();
        List<AttachFile> files = new ArrayList<>();
        // 将请求转换成
        MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
        /**
         * 获取上传的文件列表
         */
        Iterator<String> fns = mRequest.getFileNames();
        while (fns.hasNext()) {
            String s = fns.next();
            MultipartFile mFile = mRequest.getFile(s);
            if (mFile != null) {
                /**
                 * 文件名称
                 */
                String originFileName = mFile.getName();
                /**
                 * 存储目录
                 */
                // 会议附件存储目录
                String basePath = request.getSession().getServletContext().getRealPath(Constants.FILE);
                File dir = new File(basePath);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                // 后缀名
                String suffixName = originFileName.substring(originFileName.lastIndexOf(".") + 1);
                // 随机文件名称
                String randomId = Constants.createUUIDKey();
                String randomName = randomId + "." + suffixName;
                File file = new File(basePath + "/", randomName);
                try {
                    if (!file.exists()) {
                        file.createNewFile();
                    }
                    /**
                     * 存储复制文件
                     */
                    FileUtils.copyInputStreamToFile(mFile.getInputStream(), file);
                    /**
                     * 文件拷贝完成后,执行存库操作
                     */
                    try {
                        AttachFile attachFile = new AttachFile();
                        attachFile.setAttachId(Constants.createUUIDKey());
                        attachFile.setAttachName(originFileName);
                        attachFile.setAttachPath(randomName);
                        attachFiles.add(attachFile);
                    } catch (Exception ex) {
                        logger.error("上传失败:" + ex);
                        return msgEntity.fail();
                    }
                } catch (Exception e) {
                    logger.error("上传失败:" + e);
                    return msgEntity.fail();
                }
            }
        }
        logger.info("上传附件成功!");
        return msgEntity.success(attachFiles);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值