java 获取本地其他盘的文件,返回给前端

	/**
	 * 获取录音文件 返回
	 * 
	 * @param response
	 * @throws IOException
	 */
	 @RequestMapping(value = "voiceFile", method = { RequestMethod.GET, RequestMethod.POST })
     public void voiceFile(HttpServletRequest request,HttpServletResponse response) throws IOException{

    	          //读取本地录音输入流  
		    FileInputStream inputStream = new FileInputStream("c:/图片地址");  
		    int i = inputStream.available();  
		    //byte数组用于存放图片字节数据  
		    byte[] buff = new byte[i];  
		    inputStream.read(buff);  
		    //记得关闭输入流  
		    inputStream.close();  
		    //设置发送到客户端的响应内容类型  
		    response.setContentType("audio/wav");  
		    OutputStream out = response.getOutputStream();  
		    out.write(buff);  
		    //关闭响应输出流  
		    out.close();  
     }
}
   response.setContentType("audio/wav");  wav 录音文件
 response.setContentType("image/jpeg");  图片录音文件

 很多类型 查找资料


你可以使用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
发出的红包

打赏作者

q联:1120972968

感谢老铁资质

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值