JAVA直接本地HTML和TXT文件。

/**
* 直接打开本地文件
* */
private void openFile(String localFileUrl) {
String filename = "openFile.bat";

File file = new File(filename);
try {
//创建本地批处理文件
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter writer = new OutputStreamWriter(fos, "GB2312");
writer.append(localFileUrl + " start");
writer.close();
fos.close();

//执行批处理文件
Runtime.getRuntime().exec(filename);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
你可以使用Java提供的File和Scanner类来读取本地HTML文件,然后将读取到的内容返回给前端。以下是一个示例代码: ```java import java.io.File; import java.io.IOException; import java.util.Scanner; public class HtmlReader { public static String readHtmlFile(String filePath) throws IOException { StringBuilder contentBuilder = new StringBuilder(); File file = new File(filePath); try (Scanner scanner = new Scanner(file)) { while (scanner.hasNextLine()) { String line = scanner.nextLine(); contentBuilder.append(line).append("\n"); } } return contentBuilder.toString(); } } ``` 在你的Web应用程序,你可以将此方法用于读取HTML文件并将其返回给前端: ```java import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/html") public class HtmlController { @GetMapping @ResponseBody public String getHtml(HttpServletRequest request, HttpServletResponse response, @RequestParam String fileName) throws IOException { String filePath = request.getServletContext().getRealPath("/WEB-INF/html/" + fileName); return HtmlReader.readHtmlFile(filePath); } } ``` 在上面的代码,我们通过@RequestParam注释从请求参数获取HTML文件名,然后将其传递给HtmlReader.readHtmlFile()方法来读取文件并返回其内容。最后,我们使用@ResponseBody注释将内容作为响应主体返回给前端。注意,这里的文件路径使用了相对路径,我们假设HTML文件存储在Web应用程序的/WEB-INF/html目录
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值