方式1:读取模板文件,替换模板中的部分内容,生成新的文件;
public class FileUtils {
/**
* 创建html页面
* @param tempPath 模板路径
* @param aimPath 新生成的目标文件路径
* @param aimName 新生成的目标文件名称
* @param text 页面替换内容
*/
public static void CreHtml(String tempPath,String aimPath,String aimName,String text){
//读取模板
String fileContent = "";
try {
FileInputStream fileInputStream = new FileInputStream(tempPath);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
//将模板内容提取到字符串
String line = null;
while ((line = bufferedReader.readLine()) != null){
fileContent += line;
}
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
//字符替换,得到目标内容
String aimContent = fileContent.replaceAll("##content##", text);
//定义新生成的文件
String filename = aimName+".html";
filename = aimPath+"/"+filename;
try {
//文件输出
FileOutputStream fileOutputStream = new FileOutputStream(filename);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
outputStreamWriter.write(aimContent);
outputStreamWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
方式2:读取前台传递的文件MultipartFile,并将此文件保存到 本地 | 服务器 |
public Response<String> upload(MultipartFile file, String detail, String number, String username, String unit, String email,String theme) {
String filepath = null;
String filename =null;
if(file==null){
//做一些处理。。。
//然后返回一些内容
return Response.ok("成功但文件为空");
}
String originalFilename = file.getOriginalFilename();
//生成随机文件名
filename = UUID.randomUUID().toString()+originalFilename;
//此处是从yml文件里读取的保存路径,你可自己定义自己的路径
filepath = ymlConfig.getSave().getPath();
File dest = new File(filepath + filename );
try {
file.transferTo(dest);
} catch (Exception e) {
/*出现异常1.手动回滚,2.并删除文件
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
FileUtil.delFile(newName,ymlConfig.getSave().getPath());
return Response.fail("您的反馈失败,文件上传有误");*/
}
return Response.ok("操作成功");
}
方式3:使用IOUtils工具类,读取前台传递的文件MultipartFile,并将此文件保存到 本地 | 服务器 |
public Response<String> upload(MultipartFile file, String detail, String number, String username, String unit, String email,String theme) {
Response<String> response = new Response<>();
String filepath = null;
String filename =null;
if(file==null){
//做一些处理。。。
//然后返回一些内容
return Response.ok("成功但文件为空");
}
String originalFilename = file.getOriginalFilename();
filename = UUID.randomUUID().toString()+originalFilename;
//此处是从yml文件里读取的保存路径,你可自己定义自己的路径
filepath = ymlConfig.getSave().getPath();
FileOutputStream outputStream = null;
InputStream inputStream = null;
try {
outputStream = new FileOutputStream(filepath + filename );
inputStream = file.getInputStream();
IOUtils.copy(inputStream,outputStream);
inputStream.close();
outputStream.close();
} catch (Exception e) {
/*出现异常1.手动回滚,2.并删除文件
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
FileUtil.delFile(newName,ymlConfig.getSave().getPath());
return Response.fail("您的反馈失败,文件上传有误");*/
}
return Response.ok("操作成功");
}