You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
2.2 KiB
68 lines
2.2 KiB
5 months ago
|
package com.qsd.orange.controller;
|
||
|
|
||
|
import cn.hutool.core.io.IoUtil;
|
||
|
import cn.hutool.core.io.file.FileWriter;
|
||
|
import cn.hutool.system.SystemUtil;
|
||
|
import com.qsd.orange.global.R;
|
||
|
import org.springframework.web.bind.annotation.*;
|
||
|
import org.springframework.web.multipart.MultipartFile;
|
||
|
|
||
|
import javax.servlet.http.HttpServletResponse;
|
||
|
import java.io.*;
|
||
|
|
||
|
@RestController
|
||
|
@RequestMapping("system/resource/image")
|
||
|
public class SystemResourceController {
|
||
|
|
||
|
public static final String IMAGE_PATH = SystemUtil.get("user.dir");
|
||
|
|
||
|
@PostMapping
|
||
|
public R updateImage(MultipartFile file){
|
||
|
String temp = file.getName();
|
||
|
System.out.println(temp);
|
||
|
temp = "png";
|
||
|
String name = System.currentTimeMillis() + "." + temp;
|
||
|
BufferedOutputStream outputStream = null;
|
||
|
try(InputStream inputStream = file.getInputStream();) {
|
||
|
File f = new File(IMAGE_PATH, name);
|
||
|
if (!f.exists()){
|
||
|
f.createNewFile();
|
||
|
}
|
||
|
FileWriter fileWriter = new FileWriter(f);
|
||
|
outputStream = fileWriter.getOutputStream();
|
||
|
IoUtil.copy(inputStream, outputStream);
|
||
|
} catch (IOException e) {
|
||
|
e.printStackTrace();
|
||
|
return R.error();
|
||
|
}finally {
|
||
|
try {
|
||
|
outputStream.close();
|
||
|
} catch (IOException e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
return R.success().data("image", name);
|
||
|
}
|
||
|
|
||
|
@GetMapping("{name}")
|
||
|
public void show(@PathVariable("name") String name, HttpServletResponse response) {
|
||
|
FileInputStream inputStream = null;
|
||
|
try {
|
||
|
inputStream = new FileInputStream(new File(IMAGE_PATH, name));
|
||
|
response.setContentType("image/png");
|
||
|
IoUtil.copy(inputStream, response.getOutputStream());
|
||
|
} catch (FileNotFoundException e) {
|
||
|
e.printStackTrace();
|
||
|
} catch (IOException e) {
|
||
|
e.printStackTrace();
|
||
|
} finally {
|
||
|
try {
|
||
|
inputStream.close();
|
||
|
} catch (IOException e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|