package com.fgkj.controller;
|
|
import com.fgkj.util.*;
|
|
import com.fgkj.dto.User_Chart;
|
import com.fgkj.services.User_ChartService;
|
import io.swagger.annotations.Api;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.web.bind.annotation.PutMapping;
|
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RestController;
|
import sun.misc.BASE64Decoder;
|
|
import java.io.*;
|
import java.util.*;
|
import javax.annotation.Resource;
|
|
@RequestMapping("chart")
|
@RestController
|
@Api
|
public class ChartController{
|
|
private static Map<String,String> fileUtil = new HashMap<String, String>(){
|
{
|
put("logo","logo.png");
|
put("normal","home_normal.gif");
|
put("behind","home_behind.gif");
|
put("timeout","home_timeout.gif");
|
put("warn","home_warn.gif");
|
}
|
};
|
|
@Resource
|
private User_ChartService service;
|
|
// private List<File> files;
|
// private List<String> filesFileName;
|
//上传文件内容类型集合
|
// private String josn;
|
|
//执行上传功能
|
private void uploadFile(File file,String webpath) {
|
InputStream in =null;
|
OutputStream out =null;
|
try {
|
File fileLocation = new File(webpath).getParentFile();
|
//此处也可以在应用根目录手动建立目标上传目录
|
if(!fileLocation.exists()){
|
boolean isCreated = fileLocation.mkdir();
|
if(!isCreated) {
|
//目标上传目录创建失败,可做其他处理,例如抛出自定义异常等,一般应该不会出现这种情况。
|
System.out.println("创建失败!");
|
}
|
}
|
in = new FileInputStream(file);
|
File uploadFile = new File(webpath);
|
out = new FileOutputStream(uploadFile);
|
byte[] buffer = new byte[1024 * 1024];
|
int length;
|
while ((length = in.read(buffer)) > 0) {
|
out.write(buffer, 0, length);
|
}
|
out.flush();
|
}catch (FileNotFoundException e) {
|
e.printStackTrace();
|
}catch (IOException e) {
|
e.printStackTrace();
|
}finally{
|
try {
|
if (in!= null) {
|
in.close();
|
}
|
if (out != null) {
|
out.close();
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
|
@PutMapping("form")
|
public boolean uploadform(@RequestBody User_Chart uchart,@RequestBody List<File> files,@RequestBody List<String> filesFileName){
|
// User_Chart uchart=ActionUtil.getGson().fromJson(json, User_Chart.class);
|
boolean res=false;
|
String webpath=null;//上传路径
|
//System.out.println(uchart);
|
String loadpath = ActionUtil.getSession().getServletContext().getRealPath("/");
|
String root = new File(loadpath).getAbsolutePath();
|
System.out.println("root:"+root);
|
for (int i = 0; i < files.size(); i++) {
|
System.out.println("filename:"+filesFileName.get(i));
|
File file=files.get(i);
|
//循环上传每个文件
|
webpath=root+"/images/"+uchart.getChart_file()+"/"+filesFileName.get(i);
|
System.out.println(webpath);
|
//uploadFile(file,webpath);
|
}
|
System.out.println("完成");
|
//model=service.add(uchart);
|
res=true;
|
return res;
|
}
|
|
//修改班组的logo
|
@PutMapping("serverRootLogo")
|
public boolean updateServerRootLogo(HashMap<String,String> s){
|
boolean res=false;
|
boolean isSuccess = false;
|
//获取服务器上文件存放的根路径
|
String loadpath = ActionUtil.getSession().getServletContext().getRealPath("/");
|
String str = new File(loadpath).getParentFile().getAbsolutePath();
|
String root = str+"/images/";
|
// Map<String,String> s = getGson().fromJson(json, new TypeToken<HashMap<String,String>>(){}.getType());
|
String dirname = s.get("chart_file");
|
if(dirname != null && dirname.length()>0){
|
root += dirname + "/";
|
}
|
List<String> bases = new ArrayList<String>();
|
List<String> filepath = new ArrayList<String>();
|
Iterator iter = s.entrySet().iterator();
|
while (iter.hasNext()) {
|
Map.Entry entry = (Map.Entry) iter.next();
|
Object key = entry.getKey();
|
String fname = fileUtil.get(key);
|
if(fname!=null && fname.length()>0){
|
bases.add(entry.getValue().toString());
|
filepath.add(root+fname);
|
System.out.println(root+fname);
|
}
|
Object val = entry.getValue();
|
}
|
//System.out.println(bases.size());
|
//System.out.println(filepath.size());
|
for(int i=0;i<bases.size();i++){
|
String pattern = "^data:image\\/\\w+;base64,";
|
String basestr = bases.get(i).replaceFirst(pattern, "");
|
ActionUtil.generateImage(basestr,filepath.get(i));
|
}
|
res=true;
|
|
return res;
|
}
|
|
|
private void createFileByBase64(String path,String base64){
|
base64 =base64.replaceAll("data:image/jpeg;base64,", "");
|
BASE64Decoder decoder = new BASE64Decoder();
|
try {
|
// Base64解码
|
byte[] b = decoder.decodeBuffer(base64);
|
for (int i = 0; i < b.length; ++i) {
|
if (b[i] < 0) {// 调整异常数据
|
b[i] += 256;
|
}
|
}
|
// 生成jpeg图片
|
|
File file = new File(path);
|
OutputStream out = new FileOutputStream(file);
|
out.write(b);
|
out.flush();
|
out.close();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
}
|