欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot 中文件上傳下載實例代碼

 更新時間:2017年11月15日 15:21:18   作者:yshy  
Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發(fā)過程。這篇文章主要介紹了springboot 中文件上傳下載實例代碼,需要的朋友可以參考下

Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發(fā)過程。該框架使用了特定的方式來進行配置,從而使開發(fā)人員不再需要定義樣板化的配置。通過這種方式,Spring Boot致力于在蓬勃發(fā)展的快速應用開發(fā)領域(rapid application development)成為領導者。

Spring Boot特點

1. 創(chuàng)建獨立的Spring應用程序

2. 嵌入的Tomcat,無需部署WAR文件

3. 簡化Maven配置

4. 自動配置Spring

5. 提供生產(chǎn)就緒型功能,如指標,健康檢查和外部配置

6. 絕對沒有代碼生成和對XML沒有要求配置[

springboot 實現(xiàn)文件上傳下載實例代碼如下所示:

@Controller
public class FileUploadCtrl {
 @Value("${file.upload.dir}")
 private String path;
 /**
  * 實現(xiàn)文件上傳
  * */
 @RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
 @ResponseBody
 public Map<String,Object> fileUpload(@RequestParam("fileName") MultipartFile file){
  Map<String,Object> map = new HashMap<String, Object>();
  int no = 0;
  String msg = "上傳失??!";
  if(!file.isEmpty()){
   String fileName = file.getOriginalFilename();
   File dest = new File(path + "/" + fileName);
   if(!dest.getParentFile().exists()){ //判斷文件父目錄是否存在
    dest.getParentFile().mkdir();
   }
   try {
    file.transferTo(dest); //保存文件
    no = 1;
    msg = "上傳成功!";
   } catch (IllegalStateException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  map.put("no",no);
  map.put("msg", msg);
  return map;
 }
 @RequestMapping(
   value = "/fileDownload",
   method = RequestMethod.GET
 )
 public ResponseEntity<?> getGwFileContent(@RequestParam String fileName,@RequestParam int flag) {
  HttpHeaders headers = new HttpHeaders();
  headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
  String filepath = path+"/"+fileName;;
  InputStream is = null;
  try {
   headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", new String(fileName.getBytes("GBK"), "ISO8859-1")));
   if(flag==0){//表示獲取縮略圖
    File file = new File(filepath);
    filepath = path+"/xx"+fileName;
    File xxFile = new File(filepath);
    if(!xxFile.exists()){//不存在就生成縮略圖
     Thumbnails.of(file).scale(0.25f).toFile(xxFile);
    }
   }
   is = new FileInputStream(new File(filepath));
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  headers.add("Pragma", "no-cache");
  headers.add("Expires", "0");
  return ResponseEntity
    .ok()
    .headers(headers)
    .contentType(MediaType.parseMediaType("application/octet-stream"))
    .body(new InputStreamResource(is));
 }
}

總結

以上所述是小編給大家介紹的springboot 中文件上傳下載實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關文章

最新評論