起因
- 一个文件存储系统中有一个“快速文本”的功能,流程为用户在前端的textarea中添加文本内容,后台接收改字符串后生成一个临时的txt文件,使用完毕后会删掉该临时文件,为了防止用户上传超大字符串等恶意攻击,应该对字符串长度进行判断,如果太长就不会生成txt文件,并返回错误信息。这里就涉及到了Java中String字符串的长度与生成的txt文件大小之间的关系。
分析
- 这里用自己写的生成txt文件的方法对不同的字符串内容进行测试。
- generateNewText()
public static File generateNewText(String filePath,String content) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
}
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(content);
fileWriter.close();