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 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; }