今天做了文件下载
下面是代码,下载还是根据url的定位
public class DownloadAndUploadController {
@Autowired
private Upload upload;
@Autowired
private ImgMapper imgService;
@RequestMapping("/UploadImg")
@ResponseBody
public String UploadImg(MultipartFile file, HttpServletResponse response, HttpServletRequest request) throws IOException {
upload.uploadOne(staticConfig.UserMail,file);
return staticMsg.successfulUpload;
}
@RequestMapping("/DownloadImg")
@ResponseBody
public byte[] downloadImg(String fileName) throws IOException {
ImgObject imgObject=imgService.getImg(fileName);
return FileUtil.readFileStreamByBytes(ImageCompression.ImgCompression(imgObject));
}
@RequestMapping("/DownloadRealImg")
@ResponseBody
public byte[] downloadRealImg(String fileName) throws IOException {
ImgObject imgObject=imgService.getImg(fileName);
return FileUtil.readFileByBytes(imgObject.getImgPath()+imgObject.getImgId()+imgObject.getImgName().substring(imgObject.getImgName().lastIndexOf(".")));
}
@RequestMapping("/getImgList")
@ResponseBody
public String getImgList(String class2){
JSONArray jsonArray=new JSONArray();
String str2="#";
if(class2==null){
System.out.println("class2==null");
List<ImgObject> imgObjects=imgService.getUserAllImg(staticConfig.UserMail);
for (ImgObject imgObject : imgObjects) {
JSONObject jsonObject=new JSONObject();
String str=imgObject.getImgPath();
if(!str2.equals(imgObject.getImgPath().substring(str.indexOf("\")+ 1,str.indexOf("-")).substring(str.lastIndexOf("m")+2))){
str2=str.substring(str.indexOf("\")+ 1,str.indexOf("-")).substring(str.lastIndexOf("m")+2);
jsonObject.put("title",str2);
jsonObject.put("img"," http://octoberimg.viphk.ngrok.org/SmartCloudAlbum_war_exploded/Cloud/DownloadImg?fileName="+imgObject.getImgId());
jsonObject.put("imgID",imgObject.getImgId());
jsonArray.add(jsonObject);
}
}
return jsonArray.toJSONString();
}else {
System.out.println("class2="+class2);
List<ImgObject> imgObjects=imgService.getUserImgByFile(staticConfig.UserMail,class2);
if (class2.contains("-"))
{
for (ImgObject imgObject : imgObjects) {
JSONObject jsonObject=new JSONObject();
String str=imgObject.getImgPath();
str=str.substring(str.indexOf(class2));
str=str.substring(str.indexOf("\")+1);
str=str.substring(0,str.indexOf("\"));
System.out.println(str);
if (!str2.equals(str)){
str2=str;
jsonObject.put("title",str2);
jsonObject.put("img"," http://octoberimg.viphk.ngrok.org/SmartCloudAlbum_war_exploded/Cloud/DownloadImg?fileName="+imgObject.getImgId());
jsonObject.put("imgID",imgObject.getImgId());
jsonArray.add(jsonObject);
}
}
}
for (ImgObject imgObject : imgObjects) {
JSONObject jsonObject=new JSONObject();
String str=imgObject.getImgPath();
//str=str.substring(str.indexOf("-")+1).substring(0,str.indexOf("\"));
str=str.substring(str.indexOf(class2));
str=str.substring(0,str.indexOf("\"));
System.out.println(str);
if (!str2.equals(str)){
str2=str;
jsonObject.put("title",str2);
jsonObject.put("img"," http://octoberimg.viphk.ngrok.org/SmartCloudAlbum_war_exploded/Cloud/DownloadImg?fileName="+imgObject.getImgId());
jsonObject.put("imgID",imgObject.getImgId());
jsonArray.add(jsonObject);
}
}
return jsonArray.toJSONString();
}
}