上传接口完成
This commit is contained in:
@ -0,0 +1,22 @@
|
||||
package com.chinaunicom.zyhy.ebtp.supplier.base.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
import java.nio.file.Paths;
|
||||
|
||||
@Configuration
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
|
||||
@Value("${file.upload-dir}")
|
||||
private String uploadDir;
|
||||
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
String absolutePath = Paths.get(uploadDir).toAbsolutePath().toUri().toString();
|
||||
registry.addResourceHandler("/files/**")
|
||||
.addResourceLocations(absolutePath);
|
||||
}
|
||||
}
|
@ -1,7 +1,10 @@
|
||||
package com.chinaunicom.zyhy.ebtp.supplier.base.constant;
|
||||
|
||||
public interface UpConstant {
|
||||
|
||||
/**
|
||||
* 草稿
|
||||
*/
|
||||
public static final Long DRAFT = 0L;
|
||||
/**
|
||||
* 上架
|
||||
*/
|
||||
|
@ -0,0 +1,45 @@
|
||||
package com.chinaunicom.zyhy.ebtp.supplier.base.controller;
|
||||
|
||||
import com.chinaunicom.zyhy.ebtp.supplier.base.service.FileStorageService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/files")
|
||||
public class FileController {
|
||||
|
||||
@Autowired
|
||||
private FileStorageService fileStorageService;
|
||||
|
||||
@Value("${file.upload-dir}")
|
||||
private String uploadDir;
|
||||
|
||||
|
||||
|
||||
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
public Map<String, Object> upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
|
||||
String baseUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
|
||||
|
||||
String filename = fileStorageService.storeFile(file);
|
||||
String fileUrl = baseUrl+"/files/" + filename;
|
||||
|
||||
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("fileName", filename);
|
||||
response.put("fileType", file.getContentType());
|
||||
response.put("fileSize", file.getSize());
|
||||
response.put("url", fileUrl);
|
||||
response.put("filePath", uploadDir + "/"+filename);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package com.chinaunicom.zyhy.ebtp.supplier.base.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
public class FileStorageService {
|
||||
|
||||
@Value("${file.upload-dir}")
|
||||
private String uploadDir;
|
||||
|
||||
public String storeFile(MultipartFile file) {
|
||||
try {
|
||||
String originalName = StringUtils.cleanPath(file.getOriginalFilename());
|
||||
String ext = "";
|
||||
|
||||
int i = originalName.lastIndexOf(".");
|
||||
if (i >= 0) {
|
||||
ext = originalName.substring(i);
|
||||
}
|
||||
|
||||
String filename = UUID.randomUUID().toString() + ext;
|
||||
Path copyLocation = Paths.get(uploadDir).toAbsolutePath().normalize().resolve(filename);
|
||||
|
||||
Files.createDirectories(copyLocation.getParent());
|
||||
Files.copy(file.getInputStream(), copyLocation, StandardCopyOption.REPLACE_EXISTING);
|
||||
|
||||
return filename;
|
||||
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException("Could not store file: " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user