java 读取文件,写入文件,防止乱码

方式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("操作成功");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值