package com.test.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.test.commons.rest.RestErrorEnum;
import com.test.commons.rest.RestException;
import org.apache.commons.io.FileUtils;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
/**
* Json export or import.
*
* @author levi
**/
@PropertySource(value = "classpath:application.yml")
public class JsonUtil {
public static final String TEMP_PATH = "template-temp";
/**
* readJson.
*
* @param multipartFile multipartFile
* @return obj
*/
public static Map<String, String> readJson(MultipartFile multipartFile) {
Map<String, String> result = new HashMap<>();
try {
String fileName = multipartFile.getOriginalFilename();
String suffixName = fileName.substring(fileName.lastIndexOf("."));
//MultipartFile to string
File file = new File("/" + fileName);
FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), file);
String jsonString = FileUtils.readFileToString(file, "UTF-8");
if (".json".equals(suffixName) || ".txt".equals(suffixName)) {
result.put("result", jsonString);
result.put("code", "200");
result.put("message", "上传成功!");
} else {
result.put("result", "");
result.put("code", "500");
result.put("message", "请上传正确格式的.json或.txt文件!");
}
} catch (Exception e) {
e.printStackTrace();
result.put("result", "");
result.put("code", "500");
result.put("message", e.getMessage());
}
return result;
}
public static Map<String, String> readJson(File multipartFile) {
Map<String, String> result = new HashMap<>();
try {
String fileName = multipartFile.getName();
String suffixName = fileName.substring(fileName.lastIndexOf("."));
//MultipartFile to string
//File file = new File("/" + fileName);
//FileUtils.copyInputStreamToFile(new FileInputStream(file), file);
String jsonString = FileUtils.readFileToString(multipartFile, "UTF-8");
if (".json".equals(suffixName) || ".txt".equals(suffixName)) {
result.put("result", jsonString);
result.put("code", "200");
result.put("message", "上传成功!");
} else {
result.put("result", "");
result.put("code", "500");
result.put("message", "请上传正确格式的.json或.txt文件!");
}
} catch (Exception e) {
e.printStackTrace();
result.put("result", "");
result.put("code", "500");
result.put("message", e.getMessage());
}
return result;
}
/**
* exportJson.
*
* @param response response
* @param obj obj
* @param fileName fileName
*/
public static File exportJson(HttpServletResponse response, Object obj, String fileName, String uploadPath) {
try {
String jsonString = JSON.toJSONString(obj,
SerializerFeature.PrettyFormat,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteDateUseDateFormat);
//String fullPath = "/" + fileName;
File path = new File(uploadPath);
if (!path.exists()) {
path.mkdirs();
}
String fullPath = uploadPath + "/" + fileName;
File file = new File(fullPath);
Writer write = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8);
write.write(jsonString);
write.flush();
write.close();
FileInputStream fis = new FileInputStream(file);
// force-download
response.setContentType("application/force-download");
response.setHeader("Content-Disposition", "attachment;filename="
.concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8"))));
response.setCharacterEncoding("utf-8");
OutputStream os = response.getOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while ((len = fis.read(buf)) != -1) {
os.write(buf, 0, len);
}
fis.close();
os.close();
return file;
} catch (Exception e) {
throw new RestException(RestErrorEnum.PARAMETERS_ERROR, "导出模板JSON文件失败");
}
}
}
Java 导入导出JSON文件
最新推荐文章于 2025-05-08 21:35:30 发布