DataSourceController.java 2.08 KB
package com.objecteye.controller;

import com.objecteye.common.CommonResult;
import com.objecteye.handle.PictureHandle;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;

@RestController
@Api(tags = "DataSourceController", description = "数据源上传")
@RequestMapping("/upload")
@CrossOrigin
public class DataSourceController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private PictureHandle pictureHandle;

    @Value("${picture.storePath}")
    private String storePath;

    /**
     * 上传图片绝对路径到队列中
     *
     * @param path
     * @return
     */
    @ApiOperation("根据绝对路径上传数据")
    @RequestMapping(value = "/findPage", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public CommonResult uploadPicByPath(String path) {

        File file = new File(path);
        boolean directory = file.isDirectory();
        boolean exists = file.exists();
        if (directory && exists) {
            File[] files = file.listFiles();
            if (files != null && files.length > 0) {
                int length = files.length;
                for (int i = 0; i < length; i++) {
                    File file1 = files[i];
                    String name = file1.getName();
                    String newPath = storePath + name;
                    File file2 = new File(newPath);
                    file1.renameTo(file2);
                    redisTemplate.opsForList().leftPush("picPath", name);
                    pictureHandle.handlePic();
                }
            }
        }
        return CommonResult.success("{}");
    }
}