public void downloadFile(String path, String fileName) throws UpYunExcetion { try { StringBuffer url = new StringBuffer(); for (String str : fileName.split("/")) { if (str == null || str.length() == 0) { continue; } url.append(UrlCodingUtil.encodeBase64(str.getBytes("utf-8")) + "/"); } url = url.delete(url.length() - 1, url.length()); sign.setUri(url.toString()); } catch (UnsupportedEncodingException e) { LogUtil.exception(logger, e); } sign.setContentLength(0); sign.setMethod(HttpMethodEnum.GET.name()); String url = autoUrl + sign.getUri(); Map<String, String> headers = sign.getHeaders(); HttpResponse httpResponse = HttpClientUtils.getByHttp(url, headers); if (httpResponse.getStatusLine().getStatusCode() != 200) { throw new UpYunExcetion( httpResponse.getStatusLine().getStatusCode(), httpResponse.getStatusLine().getReasonPhrase()); } HttpEntity entity = httpResponse.getEntity(); try { FileUtil.saveToFile(path + "/" + fileName, entity.getContent()); } catch (Exception e) { LogUtil.exception(logger, e); } }
public void uploadFile(String fileName, FileInputStream instream, Integer fileLength) throws UpYunExcetion { try { StringBuffer url = new StringBuffer(); for (String str : fileName.split("/")) { if (str == null || str.length() == 0) { continue; } url.append(UrlCodingUtil.encodeBase64(str.getBytes("utf-8")) + "/"); } url = url.delete(url.length() - 1, url.length()); sign.setUri(url.toString()); } catch (UnsupportedEncodingException e) { LogUtil.exception(logger, e); } sign.setContentLength(fileLength); sign.setMethod(HttpMethodEnum.PUT.name()); String url = autoUrl + sign.getUri(); Map<String, String> headers = sign.getHeaders(); headers.put("mkdir", "true"); HttpResponse httpResponse = HttpClientUtils.putByHttp(url, headers, instream, fileLength); if (httpResponse.getStatusLine().getStatusCode() != 200) { throw new UpYunExcetion( httpResponse.getStatusLine().getStatusCode(), httpResponse.getStatusLine().getReasonPhrase()); } }
public void delete(String name, Boolean flag) throws UpYunExcetion { try { StringBuffer url = new StringBuffer(); for (String str : name.split("/")) { if (str == null || str.length() == 0) { continue; } url.append(UrlCodingUtil.encodeBase64(str.getBytes("utf-8")) + "/"); } if (flag) { url = url.delete(url.length() - 1, url.length()); } sign.setUri(url.toString()); } catch (UnsupportedEncodingException e) { LogUtil.exception(logger, e); } sign.setContentLength(0); sign.setMethod(HttpMethodEnum.DELETE.name()); String url = autoUrl + sign.getUri(); Map<String, String> headers = sign.getHeaders(); HttpResponse httpResponse = HttpClientUtils.deleteByHttp(url, headers); if (httpResponse.getStatusLine().getStatusCode() != 200) { throw new UpYunExcetion( httpResponse.getStatusLine().getStatusCode(), httpResponse.getStatusLine().getReasonPhrase()); } }
public void uploadFile(File file) throws UpYunExcetion { FileInputStream fis = null; try { fis = new FileInputStream(file); uploadFile(file.getName(), fis, fis.available()); } catch (FileNotFoundException e) { LogUtil.exception(logger, e); } catch (IOException e) { LogUtil.exception(logger, e); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { LogUtil.exception(logger, e); } } } }
public FileVo listFileInfo(String fileName) throws UpYunExcetion { try { StringBuffer url = new StringBuffer(); for (String str : fileName.split("/")) { if (str == null || str.length() == 0) { continue; } url.append(UrlCodingUtil.encodeBase64(str.getBytes("utf-8")) + "/"); } url = url.delete(url.length() - 1, url.length()); sign.setUri(url.toString()); } catch (UnsupportedEncodingException e) { LogUtil.exception(logger, e); } sign.setContentLength(0); sign.setMethod(HttpMethodEnum.HEAD.name()); String url = autoUrl + sign.getUri(); Map<String, String> headers = sign.getHeaders(); HttpResponse httpResponse = HttpClientUtils.headByHttp(url, headers); FileVo fileVo = null; if (httpResponse.getStatusLine().getStatusCode() != 200) { throw new UpYunExcetion( httpResponse.getStatusLine().getStatusCode(), httpResponse.getStatusLine().getReasonPhrase()); } else { fileVo = new FileVo(); for (Header header : httpResponse.getAllHeaders()) { if ("x-upyun-file-type".equals(header.getName())) { fileVo.setType(header.getValue()); } else if ("x-upyun-file-size".equals(header.getName())) { fileVo.setSize(Long.valueOf(header.getValue())); } else if ("x-upyun-file-date".equals(header.getName())) { fileVo.setCreatedAt(new Date(Long.valueOf(header.getValue()) * 1000)); } } } return fileVo; }