/**
* 將本地照片上傳至騰訊云服務上
*/
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();
}