package com.lf.server.service.all;
|
|
import com.lf.server.entity.data.MetaEntity;
|
import com.lf.server.helper.FileHelper;
|
import com.lf.server.helper.PathHelper;
|
import com.lf.server.helper.StringHelper;
|
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.LogFactory;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.File;
|
import java.io.IOException;
|
import java.lang.reflect.Field;
|
import java.sql.Timestamp;
|
import java.util.*;
|
|
/**
|
* 父上传服务
|
* @author WWW
|
*/
|
@Service
|
public class BaseUploadService {
|
@Autowired
|
public PathHelper pathHelper;
|
|
public final Log log = LogFactory.getLog(getClass());
|
|
/**
|
* 查询文件
|
*/
|
public List<MetaEntity> selectFiles(String subPath, List<String> extList) throws IOException {
|
String root = pathHelper.getConfig().getTempPath() + File.separator + subPath;
|
|
File file = new File(root);
|
if (!file.exists() && !file.isDirectory()) {
|
return null;
|
}
|
File[] files = file.listFiles();
|
if (null == files || files.length == 0) {
|
return null;
|
}
|
|
List<MetaEntity> list = new ArrayList<MetaEntity>();
|
for (File f : files) {
|
String fileName = FileHelper.getFileName(f.getPath());
|
if (null != extList) {
|
String extName = FileHelper.getExtension(fileName);
|
if (!extList.contains(extName)) {
|
continue;
|
}
|
}
|
|
double sizes = FileHelper.sizeToMb(f.length());
|
String filePath = subPath + File.separator + fileName;
|
|
MetaEntity mf = new MetaEntity();
|
mf.setName(fileName);
|
mf.setSizes(sizes);
|
mf.setPath(filePath);
|
mf.setGuid(FileHelper.getFileMd5(f.getPath()));
|
|
list.add(mf);
|
}
|
|
return list;
|
}
|
|
/**
|
* 查询目录
|
*/
|
public String selectPath() {
|
return pathHelper.getTempPathName();
|
}
|
|
/**
|
* 上传文件
|
*/
|
public <T> List<MetaEntity> uploadData(T t, String path, HttpServletRequest req, HttpServletResponse res) throws Exception {
|
StandardMultipartHttpServletRequest request = (StandardMultipartHttpServletRequest) req;
|
req.setCharacterEncoding("utf-8");
|
res.setContentType("application/json;charset=utf-8");
|
|
if (t != null) {
|
setEntity(t, request);
|
}
|
List<MetaEntity> list = getFiles(path, request);
|
|
return list;
|
}
|
|
/**
|
* 设置实体类
|
*/
|
private <T> void setEntity(T t, StandardMultipartHttpServletRequest req) {
|
Enumeration<String> enumeration = req.getParameterNames();
|
while (enumeration.hasMoreElements()) {
|
String key = enumeration.nextElement();
|
|
try {
|
Field field = t.getClass().getDeclaredField(key);
|
field.setAccessible(true);
|
String value = req.getParameter(key);
|
|
switch (field.getType().toString()) {
|
case "double":
|
field.set(t, Double.valueOf(value));
|
break;
|
case "long":
|
field.set(t, Long.valueOf(value));
|
break;
|
case "int":
|
field.set(t, Integer.valueOf(value));
|
break;
|
case "class java.sql.Timestamp":
|
field.set(t, Timestamp.valueOf(value));
|
break;
|
//case "class java.lang.String":
|
default:
|
field.set(t, value);
|
break;
|
}
|
} catch (Exception ex) {
|
log.error(ex.getMessage(), ex);
|
}
|
}
|
}
|
|
/**
|
* 获取文件
|
*/
|
public List<MetaEntity> getFiles(String subPath, StandardMultipartHttpServletRequest req) throws Exception {
|
List<MetaEntity> list = new ArrayList<MetaEntity>();
|
|
String path = pathHelper.getTempPath(subPath);
|
Iterator<String> iterator = req.getFileNames();
|
while (iterator.hasNext()) {
|
MultipartFile file = req.getFile(iterator.next());
|
if (StringHelper.isEmpty(file.getOriginalFilename())) {
|
continue;
|
}
|
|
double sizes = FileHelper.sizeToMb(file.getSize());
|
MetaEntity mf = new MetaEntity();
|
mf.setName(file.getOriginalFilename());
|
mf.setSizes(sizes);
|
mf.setPath(path + File.separator + mf.getName());
|
|
file.transferTo(new File(mf.getPath()));
|
mf.setGuid(FileHelper.getFileMd5(mf.getPath()));
|
|
list.add(mf);
|
}
|
|
return list;
|
}
|
|
/**
|
* 删除文件
|
*/
|
public Integer deleteFiles(List<MetaEntity> list) {
|
String root = pathHelper.getConfig().getTempPath();
|
|
int count = 0;
|
for (MetaEntity entity : list) {
|
if (!StringHelper.isEmpty(entity.getPath())) {
|
String file = root + File.separator + entity.getPath();
|
|
File f = new File(file);
|
if (f.exists()) {
|
f.delete();
|
count++;
|
}
|
}
|
}
|
|
return count;
|
}
|
|
/**
|
* 获取参数
|
* Enumeration<String> headers = req.getHeaderNames();
|
* Enumeration<String> attributes = req.getAttributeNames();
|
*/
|
public Map<String, String> getParams(StandardMultipartHttpServletRequest req) {
|
Map<String, String> map = new HashMap<String, String>(3);
|
|
Enumeration<String> enumeration = req.getParameterNames();
|
while (enumeration.hasMoreElements()) {
|
String key = enumeration.nextElement();
|
String value = req.getParameter(key);
|
map.put(key, value);
|
}
|
|
return map;
|
}
|
}
|