上传多个文件直接把内容转存到本地

本文介绍了一个文件上传处理的Java后端实现案例,包括文件解析、上传到本地与云端存储、生成响应脚本及将文件路径保存至数据库的过程。涉及参数验证、路径构建、文件存储与删除等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 private static final String UPLOADDIR = "uploadir";
 @ResponseBody
    @RequestMapping(value = "uploadServer")
    public String sendFileToServer(HttpServletResponse response, HttpServletRequest request) throws Exception
    {
        log.debug(Constant.ENTER_FUNCTION);
        // 解析多重请求
        MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = mRequest.getFileMap();
        String fileName = null;
        String downloadPath = null;// 返回路径
        String originalPath = null;// 原路径
        String breviaryPath = null;// 缩略图路径
        String oldName = null;// 原图名
        String breviaryName = null;// 缩略图名
        String type = request.getParameter("type").trim();
        String remark = request.getParameter("remark");
        String bigType = request.getParameter("bigType");
        String script = null;
        if (StringUtils.isEmpty(type) || StringUtils.isEmpty(remark) || StringUtils.isEmpty(bigType))
        {
            log.warn("remark and type is null" + type + "" + remark + "" + bigType);
            script = buildReturnScript(0);
            return script;
        }
        int i = 0;
        for (Iterator<Map.Entry<String, MultipartFile>> it = fileMap.entrySet().iterator(); it.hasNext();)
        {
            ++i;
            Map.Entry<String, MultipartFile> entry = it.next();
            MultipartFile mFile = entry.getValue();
            fileName = mFile.getOriginalFilename();
            if (StringUtils.isEmpty(fileName))
            {
                log.warn("fileName is not null");
                script = buildReturnScript(0);
            }
            else
            {
                // 自己服务器tomcat下路径
                String uploadDir = request.getSession().getServletContext().getRealPath("/")
                        + UploadDownloadController.UPLOADDIR;
                File file = new File(uploadDir);
                if (!file.exists())
                {
                    file.mkdir();
                }
                String filePath = uploadDir + fileName;
                file = new File(filePath);
                // 转存到本地服务器
                mFile.transferTo(file);
                String retString = BBKUploadService.updateLoad(mFile, request, SN, Constant.FLAG); // 这是上传到云存储的方法,返回图片路径
                // 获取token失败
                if (retString.equals(Constant.FLAG))
                {
                    return buildReturnScript(0);
                }
                else if (retString.equals(UPLOAD_FAIL))
                {
                    return buildReturnScript(0);
                }
                downloadPath = retString.substring(retString.indexOf("=") + 1);
                if (i == 1)
                {
                    originalPath = downloadPath;
                    oldName = fileName;
                }
                else
                {
                    breviaryPath = downloadPath;
                    breviaryName = fileName;
                }
                log.debug(Constant.EXIT_FUNCTION);
                log.debug("downloadPath = " + downloadPath);
                // 上传成功后把临时文件删除
            //    CommonUtil.deleteFile(filePath);
            }
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("oldPath", originalPath);
        params.put("breviaryPath", breviaryPath);
        params.put("type", type);
        params.put("bigType", bigType);
        params.put("breviaryName", breviaryName);
        params.put("oldName", oldName);
        params.put("remark", remark);
        // 上传成功后把路径保存到数据库
        int result = uploadDownloadService.insert(params);
        if (result == 1)
        {
            script = buildReturnScript(result);
        }
        else
        {
            script = buildReturnScript(0);
        }


        return script;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值