Commit 16eab68a by 吴学德

添加图片上传功能

parent 6977221d
......@@ -7,6 +7,7 @@
/gdtel-gztel-jyyy-service-manager/target/
/gdtel-gztel-jyyy-task/target/
/logs/
/BackgroundImg/
# Compiled class file
*.class
......
......@@ -35,4 +35,24 @@ public class FileUtil {
}
return true;
}
public static boolean makefile1(String path, MultipartFile file,String filename){
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
File savePath = new File(dir, filename);
OutputStream os = null;
try {
os = new FileOutputStream(savePath);
os.write(file.getBytes());
os.flush();
} catch (IOException e) {
log.error("上传文件失败:" + e.getMessage(), e);
return false;
} finally {
IOUtils.closeQuite(os);
}
return true;
}
}
......@@ -17,12 +17,17 @@ import com.winsun.utils.MyBatisPlusUpdateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
/**
......@@ -44,6 +49,9 @@ public class PackageController extends BaseController {
private static String FILENAME="adv_img.jpg";
private static String XIAOTUFILENAME="logo.png";
@Autowired
public PackageController(PackageMapper packageMapper,SchoolPackageMapper schoolPackageMapper) {
PackageController.packageMapper = packageMapper;
......@@ -174,20 +182,117 @@ public class PackageController extends BaseController {
@Permission(menuname = "上传背景图", value = "backgroundUpload", method = RequestMethod.POST)
public ResponseData<String>backgroundUpload(@RequestParam(value = "file") MultipartFile file) {
EntityWrapper<Package> packagewrapper = new EntityWrapper<>();
packagewrapper.setSqlSelect("max(id) as id");
List<Map<String, Object>> selectMaps = packageMapper.selectMaps(packagewrapper);
String id = selectMaps.get(0).get("id").toString();
Integer fileid = Integer.valueOf(id)+1;
public ResponseData<String>backgroundUpload(@RequestParam(value = "file") MultipartFile file,@RequestParam(value = "id",required = false) String id) {
//路径BackgroundImg/images/pkg/
String backgroundpath = FilePath.BACKGROUNDIMG.getValue()+"/"+DEFAULTPATH+"/"+fileid.toString();
String backgroundpath= FilePath.BACKGROUNDIMG.getValue()+"/"+DEFAULTPATH+"/";
if (StringUtils.isBlank(id)){
EntityWrapper<Package> packagewrapper = new EntityWrapper<>();
packagewrapper.setSqlSelect("max(id) as id");
List<Map<String, Object>> selectMaps = packageMapper.selectMaps(packagewrapper);
id = selectMaps.get(0).get("id").toString();
Integer fileid = Integer.valueOf(id)+1;
backgroundpath = backgroundpath +fileid.toString();
}else {
backgroundpath = backgroundpath+id;
}
boolean makefile = FileUtil.makefile(backgroundpath, file,FILENAME);
if (!makefile){
return ResponseData.error("上传失败!");
}
return ResponseData.success("上传成功" );
}
//xiaotudUpload
@Permission(menuname = "上传小图", value = "xiaotuUpload", method = RequestMethod.POST)
public ResponseData<String>xiaotuUpload(@RequestParam(value = "file") MultipartFile file,@RequestParam(value = "id",required = false) String id) {
String backgroundpath= FilePath.BACKGROUNDIMG.getValue()+"/"+DEFAULTPATH+"/";
if (StringUtils.isBlank(id)){
EntityWrapper<Package> packagewrapper = new EntityWrapper<>();
packagewrapper.setSqlSelect("max(id) as id");
List<Map<String, Object>> selectMaps = packageMapper.selectMaps(packagewrapper);
id = selectMaps.get(0).get("id").toString();
Integer fileid = Integer.valueOf(id)+1;
backgroundpath = backgroundpath +fileid.toString();
}else {
backgroundpath = backgroundpath+id;
}
boolean makefile = FileUtil.makefile(backgroundpath, file,XIAOTUFILENAME);
if (!makefile){
return ResponseData.error("上传失败!");
}
return ResponseData.success("上传成功" );
}
//menuname = "展示背景图"
@RequestMapping( value = "getbackground/{id}",produces = MediaType.IMAGE_JPEG_VALUE)
public byte[] getbackground(@PathVariable(value = "id") String id) {
byte[] bytes={};
if (!StringUtils.isBlank(id)){
EntityWrapper<Package> packagewrapper = new EntityWrapper<>();
packagewrapper.eq("id", id);
packagewrapper.setSqlSelect("adv_img as imgurl");
List<Map<String, Object>> selectMaps = packageMapper.selectMaps(packagewrapper);
if (!CollectionUtils.isEmpty(selectMaps)){
String imgurl = selectMaps.get(0).get("imgurl").toString();
imgurl=FilePath.BACKGROUNDIMG.getValue()+"/"+imgurl;
File file = new File(imgurl);
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
bytes = new byte[0];
try {
bytes = new byte[inputStream.available()];
} catch (IOException e) {
e.printStackTrace();
}
try {
inputStream.read(bytes, 0, inputStream.available());
} catch (IOException e) {
e.printStackTrace();
}
return bytes;
}
}
return bytes;
}
//menuname = "展示小图"
@RequestMapping( value = "getxiaotu/{id}",produces = MediaType.IMAGE_JPEG_VALUE)
public byte[] getxiaotu(@PathVariable(value = "id") String id) {
byte[] bytes={};
if (!StringUtils.isBlank(id)){
EntityWrapper<Package> packagewrapper = new EntityWrapper<>();
packagewrapper.eq("id", id);
packagewrapper.setSqlSelect("logo as imgurl");
List<Map<String, Object>> selectMaps = packageMapper.selectMaps(packagewrapper);
if (!CollectionUtils.isEmpty(selectMaps)){
String imgurl = selectMaps.get(0).get("imgurl").toString();
imgurl=FilePath.BACKGROUNDIMG.getValue()+"/"+imgurl;
File file = new File(imgurl);
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
bytes = new byte[0];
try {
bytes = new byte[inputStream.available()];
} catch (IOException e) {
e.printStackTrace();
}
try {
inputStream.read(bytes, 0, inputStream.available());
} catch (IOException e) {
e.printStackTrace();
}
return bytes;
}
}
return bytes;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment