packagecn.ycmedia.controller;importjava.io.File;importjava.io.IOException;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.RestController;importcom.qiniu.common.QiniuException;importcom.qiniu.http.Response;importcom.qiniu.storage.UploadManager;importcom.qiniu.util.Auth;/***@author朱良兴
* 七牛 图片服务器 操作展示层
**/@RestControllerpublic classQiniuImgController {
UploadManager um= newUploadManager();public static final String AK="rzPOFiue_68j1lOwApPw4qjSxf8BkXB1CGJF6R6j";public static final String SK="TMqFfkz-2ASmgpk051gqPOx-sRdwvZHgbZAmwQ0g";
Auth auth=Auth.create(AK, SK);/***@paramfileName 文件价地址
*@paramdirBundle 文件目录
*@return*@throwsException*/@RequestMapping(value= "upload", method =RequestMethod.GET)public String uploadImgFile(@RequestParam("fileName")String fileName,
@RequestParam("dirBundle")String dirBundle) throwsException{
String token=auth.uploadToken(dirBundle);
showAllFiles(newFile(fileName),token);return null;
}public void upload(String filePath ,String token) throwsIOException{try{//调用put方法上传
Response res = um.put(filePath, null,token);//打印返回的信息
System.out.println(res.bodyString());
}catch(QiniuException e) {
Response r=e.response;//请求失败时打印的异常的信息
System.out.println(r.toString());try{//响应的文本信息
System.out.println(r.bodyString());
}catch(QiniuException e1) {//ignore
}
}
}public void showAllFiles(File dir ,String token) throwsException{
File[] fs=dir.listFiles();for(int i=0; i
showAllFiles(fs[i],"");
}catch(Exception e){
}
}else{
System.err.println(fs[i].getAbsolutePath());//调用put方法上传
Response res =um.put(fs[i].getAbsolutePath(), fs[i].getName(),token);//打印返回的信息
System.out.println(res.bodyString());
}
}
}
}