日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

java 文件下载方法_【工具类】Java后台上传下载文件的几种方式

發布時間:2025/3/8 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 文件下载方法_【工具类】Java后台上传下载文件的几种方式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

/**

* 將本地照片上傳至騰訊云服務上

*/

public void uploadImage(String localImagePath) throws Exception {

// 1.將訂單照片上傳至騰訊地圖眾包側提供的云服務上

try {

File imageFile = new File(localImagePath);

if (imageFile.exists()) {

String url = "http://" + mapVendor.getHost() + mapVendorImageDetail.getBackUrl();

HttpHeaders headers = new HttpHeaders();

headers.set("Content-Type", "image/jpeg");

headers.add("Host", mapVendor.getHost());

headers.add("Authorization", mapVendorImageDetail.getBackAuth());

restTemplate.put(url, new HttpEntity(new FileSystemResource(imageFile), headers));

}

} catch (RestClientException e) {

//使用捕獲異常來處理返回的非200的狀態響應

logger.error("send image [" + localImagePath + "] to tx error", e);

}

}

/**

* 以流的方式下載

* @param path 欲下載的文件的路徑

* @param response 響應內容

*

*/

public void download(String path, HttpServletResponse response) {

try {

// path是指欲下載的文件的路徑

File file = new File(path);

// 取得文件名

String filename = file.getName();

// 取得文件的后綴名

String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();

// 獲取下載文件的輸入流

InputStream inputStream = new BufferedInputStream(new FileInputStream(file));

byte[] buffer = new byte[inputStream.available()];

inputStream.read(buffer);

inputStream.close();

// 清空response

response.reset();

// 重新設置response

response.setContentType("application/octet-stream");

response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));

response.addHeader("Content-Length", "" + file.length());

// 寫入下載文件的輸出流

OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());

outputStream.write(buffer);

outputStream.flush();

outputStream.close();

} catch (IOException ex) {

//TODO 記錄日志并做相關業務

}

}

/**

* 把網絡上的圖片保存到本地

*/

public void downloadNet() throws Exception {

// 下載網絡文件

int byteSum = 0;

int byteRead;

URL url = new URL("https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png");

URLConnection conn = url.openConnection();

InputStream inputStream = conn.getInputStream();

FileOutputStream fileOutputStream = new FileOutputStream("c:/logo.gif");

// 存放讀取的字節

byte[] buffer = new byte[1204];

while ((byteRead = inputStream.read(buffer)) != -1) {

byteSum += byteRead;

System.out.println(byteSum);

fileOutputStream.write(buffer, 0, byteRead);

}

}

/**

* 把網絡上的圖片保存到本地并支持在線打開

*/

public void downLoad(String filePath, HttpServletResponse response, boolean isOnLine) throws Exception {

File f = new File(filePath);

if (!f.exists()) {

response.sendError(404, "File not found!");

return;

}

BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));

byte[] buf = new byte[1024];

int len = 0;

response.reset(); // 非常重要

if (isOnLine) { // 在線打開方式

URL u = new URL("file:///" + filePath);

response.setContentType(u.openConnection().getContentType());

response.setHeader("Content-Disposition", "inline; filename=" + f.getName());

// 文件名應該編碼成UTF-8

} else { // 純下載方式

response.setContentType("application/x-msdownload");

response.setHeader("Content-Disposition", "attachment; filename=" + f.getName());

}

OutputStream out = response.getOutputStream();

while ((len = br.read(buf)) > 0)

out.write(buf, 0, len);

br.close();

out.close();

}

總結

以上是生活随笔為你收集整理的java 文件下载方法_【工具类】Java后台上传下载文件的几种方式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。