数据处理,前期图片文件名中包含汉字,在访问图片时出现乱码情况,无法匹配,遂写此方法改之,将数据库中文件名和本地图片名一致,即可。
@RequestMapping("/test")
public class TestController {
private final GgSpdmMapper ggSpdmMapper;
public TestController(GgSpdmMapper ggSpdmMapper) {
this.ggSpdmMapper = ggSpdmMapper;
}
@RequestMapping(value="/changeSp",method = RequestMethod.POST)
public String changeSp () throws IOException {
List filterName = new ArrayList();
filterName.add("jpeg");
filterName.add("bmp");
filterName.add("jpg");// 自己可以定义
String dist = "D:\\LenovoSoftstore\\newSoft\\apache-tomcat-7.0.100-windows-x64\\" +
"apache-tomcat-7.0.100\\webapps\\allianceSys\\images\\";
//String dist = "D:\\apache-tomcat-6.0.45\\webapps\\allianceSys\\images\\";
List data = getFileList(dist, filterName);
System.out.println(data);
QueryWrapper<GgSpdm> queryWrapper = new QueryWrapper<>();
queryWrapper.isNotNull("cptp1");
List<GgSpdm> list = ggSpdmMapper.selectList(queryWrapper);
for(int i=0;i<list.size();i++){
for(int j=0;j<data.size();j++){
String name = list.get(i).getCptp1().substring(8,list.get(i).getCptp1().length());
if(name.equals(data.get(j))){
System.out.println(list.get(i).getTxm()+"-------匹配成功");
String spmc = list.get(i).getTxm()+"_"+String.valueOf((int)((Math.random()*9+1)*1000));
GgSpdm gg = new GgSpdm();
gg.setCptp1("/images/"+spmc+".jpg");
QueryWrapper<GgSpdm> queryWrapper1 = new QueryWrapper<>();
queryWrapper1.eq("txm",list.get(i).getTxm());
int k = ggSpdmMapper.update(gg,queryWrapper1);
changeSpmc(dist+data.get(j) , spmc);
}
}
}
return JsonResponseUtils.success("",dist);
}
//修改文件名称
public static void changeSpmc(String path , String spmc) throws IOException {
File f = new File(path);
if (!f.exists()) { // 判断原文件是否存在(防止文件名冲突)
return;
}
spmc = spmc.trim();
if ("".equals(spmc) || spmc == null) // 文件名不能为空
return;
String newFilePath = null;
if (f.isDirectory()) { // 判断是否为文件夹
newFilePath = path.substring(0, path.lastIndexOf("\\")) + "\\" + spmc;
} else {
newFilePath = path.substring(0, path.lastIndexOf("\\")) + "\\" + spmc
+ path.substring(path.lastIndexOf("."));
}
File nf = new File(newFilePath);
try {
f.renameTo(nf); // 修改文件名
} catch (Exception err) {
err.printStackTrace();
}
}
// 或得扩展名
public static String getExtendName(String fileName)
{
int index = fileName.lastIndexOf(".");
if (index != -1)
{
return fileName.substring(index + 1);
}
return null;
}
// 获得文件列表
public static List getFileList(String path, List filterName)
{
File file = new File(path);
List list = new ArrayList();
String[] fileList = file.list();
for (String s : fileList)
{
String extendName = getExtendName(s);
if (filterName.contains(extendName))
{
list.add(s);
}
}
return list;
}
}