try { long fileLength = new File(downLoadPath).length(); response.setContentType("application/x-msdownload;"); response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1")); response.setHeader("Content-Length", String.valueOf(fileLength)); bis = new BufferedInputStream(new FileInputStream(downLoadPath)); bos = new BufferedOutputStream(response.getOutputStream()); byte[] buff = new byte[2048]; int bytesRead; while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { bos.write(buff, 0, bytesRead); } } catch (Exception e) { e.printStackTrace();请详细解释每一行代码
时间: 2023-06-26 15:05:55 浏览: 134
这段代码主要是用于将服务器上的文件下载到客户端,以下是每一行代码的详细解释:
```java
long fileLength = new File(downLoadPath).length();
```
获取文件的长度,即文件大小。
```java
response.setContentType("application/x-msdownload;");
```
设置响应头的 Content-Type,表示响应的内容类型为下载文件。
```java
response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));
```
设置响应头的 Content-disposition,表示将文件作为附件下载,并指定文件名。
```java
response.setHeader("Content-Length", String.valueOf(fileLength));
```
设置响应头的 Content-Length,表示响应内容的长度为文件大小。
```java
bis = new BufferedInputStream(new FileInputStream(downLoadPath));
bos = new BufferedOutputStream(response.getOutputStream());
```
创建 BufferedInputStream 和 BufferedOutputStream 对象,分别用于读取文件和向浏览器发送响应。
```java
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
```
将文件内容写入响应输出流,每次读取 2048 字节。循环读取文件内容并写入输出流,直到文件结束。同时,每次读取的字节数可能小于 2048 字节,所以需要记录实际读取的字节数 bytesRead,并只写入 bytesRead 个字节。最终,通过输出流将文件内容发送到浏览器进行下载。如果出现异常,则打印异常信息。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![cs](https://img-home.csdnimg.cn/images/20250102104920.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
Excel Upload
<input type="file" name="ExcelFileUpload" id="ExcelFileUpload" FileLength="4" AllowExtention="*" NotAllowExtension="exe|bat" />
<input type="submit" name="ExcelUploadButton" value="上傳" onclick="timedMsg();WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ExcelUploadButton", "", true, "", "", false, false))" id="ExcelUploadButton" />
請使用 Excel 『 3 』 ,使用錯誤的範本將會上傳失敗
>>Reason_code代碼對照表.xlsx<<
在這個代碼中在Excel Upload和上傳中閒空白部分,用鼠標點擊就會彈出一個窗口,怎麽控制鼠標點擊這個地方
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)