栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java本地文件上传

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java本地文件上传

只希望能帮到正在编程路上的你

先配置yml文件,如果是线上就用/user

#文件上传
upload:
  filePath: D:/
#  filePath: /usr/java/image

业务接口

package com.maxed.service.common;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public interface QuestionService {
    List uploadImg(MultipartFile[] files, HttpServletRequest request);
}

业务实现

package com.maxed.service.common;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class QuestionServiceImpl implements QuestionService {

    @Value("${upload.filePath}")
    private String filePath;
    private static boolean running = false;

    @Override
    public List uploadImg(MultipartFile[] files, HttpServletRequest request) {
        List images = new ArrayList<>();
        for (MultipartFile file : files) {
            //获取绝对路径
            String realPath = filePath;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");
            String format = sdf.format(new Date());
            //文件存放的目录
            File folder = new File(realPath + format);
            if (!folder.isDirectory()) {
                folder.mkdirs();
            }
            String oldName = file.getOriginalFilename();
            //文件后缀
            String suffix = oldName.substring(oldName.lastIndexOf("."), oldName.length());
            //文件新名字
            String newName = UUID.randomUUID().toString() + suffix;
            try {
                File targetFile = new File(folder, newName);
                if (!targetFile.exists()) {
                    targetFile.mkdirs();
                } else {
                    targetFile.delete();
                }
                file.transferTo(targetFile);
//                String filePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/uploadFile/" + format + newName;
                String filePath = request.getScheme() + "://hotel_api.xiyang.shop/uploadFile/" + format + newName;
                images.add(filePath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return images;
    }
}

控制器

import com.maxed.adapter.IgnoreSecurity;
import com.maxed.error.Result;
import com.maxed.error.ResultUtil;
import com.maxed.service.common.QuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/upload")
@Api(value = "本地图片上传", tags = {"本地图片上传"})
@Slf4j
public class LocalHostUploadController {

    @Resource
    QuestionService questionService;

    @PostMapping("/uploadMultipleFile")
    @ApiOperation(value = "文件上传")
    @IgnoreSecurity
    @ResponseBody
    public Result uploadImg(@RequestParam("file") MultipartFile[] files, HttpServletRequest request) throws IOException {
        List strings = questionService.uploadImg(files, request);
        return ResultUtil.success(strings);
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/678618.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号