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 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 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; }
public Long usage() throws UpYunExcetion { sign.setUri("?usage"); 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); String resultStr = null; if (httpResponse.getStatusLine().getStatusCode() != 200) { throw new UpYunExcetion( httpResponse.getStatusLine().getStatusCode(), httpResponse.getStatusLine().getReasonPhrase()); } else { try { resultStr = EntityUtils.toString(httpResponse.getEntity()); } catch (Exception e) { e.printStackTrace(); } } return Long.valueOf(resultStr); }
public List<FileVo> listFile(String folderName) throws UpYunExcetion { sign.setUri(folderName); 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); String resultStr = null; if (httpResponse.getStatusLine().getStatusCode() != 200) { throw new UpYunExcetion( httpResponse.getStatusLine().getStatusCode(), httpResponse.getStatusLine().getReasonPhrase()); } else { try { resultStr = EntityUtils.toString(httpResponse.getEntity()); } catch (Exception e) { e.printStackTrace(); } } String[] lines = resultStr.split("\n"); String[] columns; List<FileVo> fileVoList = new ArrayList<FileVo>(); FileVo fileVo; for (String line : lines) { columns = line.split("\t"); fileVo = new FileVo(); fileVo.setName(UrlCodingUtil.decodeBase64(columns[0])); fileVo.setIsFile(columns[1]); fileVo.setSize(Long.valueOf(columns[2])); fileVo.setUpdatedAt(new Date(Long.valueOf(columns[3]) * 1000)); fileVoList.add(fileVo); } return fileVoList; }