com.jeesuite.filesystem.FileSystemClient Java Examples

The following examples show how to use com.jeesuite.filesystem.FileSystemClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MusicController.java    From feiqu-opensource with Apache License 2.0 5 votes vote down vote up
@ResponseBody
    @PostMapping("upload")
    public Object upload(HttpServletRequest request, HttpServletResponse response, MultipartFile file){
        BaseResult result = new BaseResult();
        try{


            FqUserCache fqUser = webUtil.currentUser(request,response);
            if(fqUser == null){
                result.setResult(ResultEnum.USER_NOT_LOGIN);
                return result;
            }
            long size = file.getSize();
            if(size >  10*1000 * 1024){
                result.setResult(ResultEnum.FILE_TOO_LARGE);
                result.setMessage("上传文件大小不要超过10M");
                return result;
            }
            String musicUrl = "";
            String fileName = file.getOriginalFilename();
            String time = DateFormatUtils.format(new Date(), "yyyyMMdd");
            String path = request.getSession().getServletContext().getRealPath("upload") + File.separator + time;
            File localFile = new File(path, fileName);
            if (!localFile.exists()) {
                localFile.mkdirs();
            }
            //MultipartFile自带的解析方法
            file.transferTo(localFile);

//            String time = DateFormatUtils.format(new Date(),"yyyy/MM/dd");
            musicUrl = FileSystemClient.getClient("aliyun").upload("music/"+fileName,localFile);
//            aliyunOssClient.putObject(CommonConstant.ALIYUN_OSS_BUCKET_NAME,"music/"+fileName,file.getInputStream());
//            String musicUrl = CommonConstant.ALIOSS_URL_PREFIX+"/music/"+fileName;
            result.setData(musicUrl);
        }catch (Exception e){
            logger.error("upload music error",e);
            result.setResult(ResultEnum.FAIL);
        }
        return result;
    }
 
Example #2
Source File: FileController.java    From oneplatform with Apache License 2.0 5 votes vote down vote up
@RequestMapping(value = "uptoken", method = RequestMethod.GET)
	@ApiOperation(value = "生成上传凭证")
	@ApiImplicitParams({
		 @ApiImplicitParam(paramType="query",name="appId",dataType="String",required=true,value="应用ID"),
		 @ApiImplicitParam(paramType="query",name="group",dataType="String",required=true,value="对应配置中心配置上传组名(如:public,private等)"),
		 @ApiImplicitParam(paramType="query",name="fileName",dataType="String",required=false,value="上传文件名(覆盖上传必填)"),
		 @ApiImplicitParam(paramType="query",name="uploadDir",dataType="String",required=false,value="上传目录"),
		 @ApiImplicitParam(paramType="query",name="deleteAfterDays",dataType="Integer",required=false,value="上传多少天自动删除")
	})
	public @ResponseBody Map<String, Object> getUploadToken(HttpServletResponse response
			,@RequestParam(value="appId") String appId
			,@RequestParam(value="group") String group
			,@RequestParam(value="fileName",required = false) String fileName
			,@RequestParam(value="uploadDir") String uploadDir
			,@RequestParam(value="deleteAfterDays",required = false) Integer deleteAfterDays) {
		
		//TODO 验证 appid
		
//		if("redirect".equals(group)){
//			Map<String, Object> map = new HashMap<>();
//			map.put("uptoken", "12345556f");
//			map.put("uploadDir", uploadDir);
//			return map;
//		}
		UploadTokenParam param = new UploadTokenParam();
		param.setFileName(fileName);
		param.setUploadDir(uploadDir);
		param.setDeleteAfterDays(deleteAfterDays);
		Map<String, Object> map = FileSystemClient.getClient(group).createUploadToken(param);
		map.put("appId", appId);
		return map;
	}
 
Example #3
Source File: FileController.java    From oneplatform with Apache License 2.0 5 votes vote down vote up
@ApiOperation(value = "获取文件地址", notes = "获取文件地址")
@RequestMapping(value = "geturl", method = RequestMethod.GET)
public @ResponseBody WrapperResponse<String> getUploadFileUrl(HttpServletResponse response
		,@RequestParam("group") String group
		,@RequestParam(value="file") String fileKey) {
	
	String downloadUrl = FileSystemClient.getClient(group).getDownloadUrl(fileKey);
	return new WrapperResponse<>(downloadUrl);
}
 
Example #4
Source File: FileController.java    From oneplatform with Apache License 2.0 5 votes vote down vote up
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ApiOperation(value="上传文件")
@ApiImplicitParams({
	@ApiImplicitParam(paramType="query",name="appId",dataType="String",required=true,value="应用ID"),
	 @ApiImplicitParam(paramType="form",name="group",dataType="String",required=true,value="对应配置中心配置上传组名(如:public,private等)"),
	 @ApiImplicitParam(paramType="form",name="fileName",dataType="String",required=true,value="文件原名称"),
	 @ApiImplicitParam(paramType="form",name="file",dataType="File",required=true,value="文件内容")
})
public @ResponseBody UploadResult uploadConfigFile(@RequestParam("file") MultipartFile file,@RequestParam(value="appId") String appId,@RequestParam("group") String group,@RequestParam("fileName") String originFileName){
	try {
		
		String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1).toLowerCase();
		if(!allow_upload_suffix.contains(suffix)){
			throw new JeesuiteBaseException(9999, "不允许上传该文件类型");
		}
		String fileKey = UUID.randomUUID().toString() + "."+suffix;
		//file.transferTo(new File("/Users/jiangwei/"+fileKey));
		
		FileSystemClient client = FileSystemClient.getClient(group);
		String url = client.upload(fileKey, file.getBytes());
		
		UploadFileEntity entity = new UploadFileEntity();
		entity.setAppId(appId);
		entity.setGroupName(group);
		entity.setFileName(originFileName);
		entity.setFileUrl(url);
		entity.setMimeType(file.getContentType());
		entity.setProvider(client.getProvider().name());
		entity.setCreatedAt(new Date());
		uploadFileMapper.insertSelective(entity);
           
		return new UploadResult(url, originFileName);
	} catch (Exception e) {
		e.printStackTrace();
		throw new JeesuiteBaseException(ExceptionCode.SYSTEM_ERROR.code, "上传失败");
	}
}
 
Example #5
Source File: FileController.java    From oneplatform with Apache License 2.0 5 votes vote down vote up
@ApiOperation(value = "获取文件地址", notes = "获取文件地址")
@RequestMapping(value = "geturl/{id}", method = RequestMethod.GET)
public @ResponseBody WrapperResponse<String> getUploadFileUrlById(@PathVariable("id") int id) {
	UploadFileEntity uploadFileEntity = uploadFileMapper.selectByPrimaryKey(id);
	String downloadUrl = null;
	if(uploadFileEntity != null){			
		downloadUrl = FileSystemClient.getClient(uploadFileEntity.getGroupName()).getDownloadUrl(uploadFileEntity.getFileUrl());
	}else{
		throw new JeesuiteBaseException(4001, "文件不存在");
	}
	return new WrapperResponse<>(downloadUrl);
}
 
Example #6
Source File: FileController.java    From oneplatform with Apache License 2.0 5 votes vote down vote up
@ApiOperation(value = "删除文件")
@RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
@ApiPermOptions(perms = PermissionType.Authorized)
   public @ResponseBody WrapperResponse<String> deleteFile(@PathVariable("id") int id) {
	
	UploadFileEntity uploadFileEntity = uploadFileMapper.selectByPrimaryKey(id);
	if(uploadFileEntity != null){
		FileSystemClient.getClient(uploadFileEntity.getGroupName()).delete(uploadFileEntity.getFileUrl());
	}
	return new WrapperResponse<>();
}
 
Example #7
Source File: AliyunOssService.java    From feiqu-opensource with Apache License 2.0 4 votes vote down vote up
/**
	 * 签名生成
	 * @return
	 */
	public JSONObject policy() {

		FileSystemClient fileSystemClient = FileSystemClient.getClient("aliyun");
		UploadTokenParam uploadTokenParam = new UploadTokenParam();
		// 存储目录
		String dir = DateUtil.format(new Date(),"yyyy/MM/dd");
		uploadTokenParam.setUploadDir(dir);
		// 签名有效期
		long expireEndTime = CommonConstant.ALIYUN_OSS_EXPIRE;
		uploadTokenParam.setExpires(expireEndTime);
		uploadTokenParam.setCallbackUrl(CommonConstant.ALIYUN_OSS_CALLBACK);
		uploadTokenParam.setFsizeMax(CommonConstant.ALIYUN_OSS_MAX_SIZE * 1024 * 1024);
		Map<String, Object> map =  fileSystemClient.createUploadToken(uploadTokenParam);
// 提交节点
		String action = "http://" + CommonConstant.ALIYUN_OSS_BUCKET_NAME + "." + CommonConstant.ALIYUN_OSS_ENDPOINT;
		map.put("action",action);
		map.put("myDomain", CommonConstant.ALIOSS_URL_PREFIX);
		JSONObject result = new JSONObject(map);
		return result;

		/*Date expiration = new Date(expireEndTime);
		// 文件大小
		long maxSize = CommonConstant.ALIYUN_OSS_MAX_SIZE * 1024 * 1024;
		// 回调
		JSONObject callback = new JSONObject();
		callback.put("callbackUrl", CommonConstant.ALIYUN_OSS_CALLBACK);
		callback.put("callbackBody", "filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}");
		callback.put("callbackBodyType", "application/x-www-form-urlencoded");
		// 提交节点
		String action = "http://" + CommonConstant.ALIYUN_OSS_BUCKET_NAME + "." + CommonConstant.ALIYUN_OSS_ENDPOINT;
		try {
			PolicyConditions policyConds = new PolicyConditions();
			policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, maxSize);
			policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);
			String postPolicy = aliyunOssClient.generatePostPolicy(expiration, policyConds);
			byte[] binaryData = postPolicy.getBytes("utf-8");
			String policy = BinaryUtil.toBase64String(binaryData);
			String signature = aliyunOssClient.calculatePostSignature(postPolicy);
			String callbackData = BinaryUtil.toBase64String(callback.toString().getBytes("utf-8"));
			// 返回结果
			result.put("OSSAccessKeyId", aliyunOssClient.getCredentialsProvider().getCredentials().getAccessKeyId());
			result.put("policy", policy);
			result.put("signature", signature);
			result.put("dir", dir);
			result.put("callback", callbackData);
			result.put("myDomain", CommonConstant.ALIOSS_URL_PREFIX);
			result.put("action", action);
		} catch (Exception e) {
			LOGGER.error("签名生成失败", e);
		}
		return result;*/
	}
 
Example #8
Source File: FileSystemClientTest.java    From jeesuite-libs with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
	UploadTokenParam param = new UploadTokenParam();
	Map<String, Object> token = FileSystemClient.getClient("xxxx2").createUploadToken(param);
	
	System.out.println(token);
}