最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于springboot實(shí)現(xiàn)文件上傳

 更新時(shí)間:2020年11月08日 13:52:53   作者:Tyler Yue  
這篇文章主要為大家詳細(xì)介紹了基于springboot實(shí)現(xiàn)文件上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了基于springboot的文件上傳的具體代碼,供大家參考,具體內(nèi)容如下

第一步:在vo包下創(chuàng)建上傳前端響應(yīng)類(lèi)

import com.alibaba.druid.filter.AutoLoad;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 上傳響應(yīng)參數(shù)
 * @param <E>
 */
//以下是lombok插件注解
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Resp<E> {
 //返回狀態(tài)碼 如 200 403
 private String code;
 //返回信息
 private String Msg;
 //也可定義為 Object body 都表示任意類(lèi)型的意思
 private E body;//模板類(lèi)型

 /**
  * 成功時(shí)候方法
  * @param body
  * @param <E>
  * @return
  */
 public static<E> Resp<E> success(E body){
  return new Resp<E>("200","上傳成功!",body);
 }

 /**
  * 上傳失敗時(shí)的方法
  * @param code
  * @param msg
  * @param <E>
  * @return
  */
 public static<E> Resp<E> fail(String code,String msg){
  return new Resp<E>(code,msg,null);
 }
}

第二步:在controller層接收前端上傳的文件

import com.qf.springboot_ssm_day02.service.UploadService;
import com.qf.springboot_ssm_day02.vo.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller

public class uploadController {
 @Autowired
 private UploadService uploadService;
 @RequestMapping(value = "upload",method = RequestMethod.POST)
 @ResponseBody
 //返回類(lèi)型根據(jù)自定義的返回類(lèi)型 不一定和我一樣 
 public Resp<String> upload(@RequestParam("file")MultipartFile file){

  return uploadService.upload(file);
 }

}

第三步:在servcie包下建立upload接口及其實(shí)現(xiàn)類(lèi)處理業(yè)務(wù)

import com.qf.springboot_ssm_day02.vo.Resp;
import org.springframework.web.multipart.MultipartFile;
/**
*上傳業(yè)務(wù)類(lèi)
*/
public interface UploadService {
 //上傳接口
 Resp<String > upload(MultipartFile file);
}
import com.qf.springboot_ssm_day02.service.UploadService;
import com.qf.springboot_ssm_day02.vo.Resp;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * 上傳業(yè)務(wù)實(shí)現(xiàn)類(lèi)
 */
@Service
public class UploadServiceImpl implements UploadService {

 @Override
 public Resp<String> upload(MultipartFile file) {
  //判斷上傳的文件是不是空
  if (file.isEmpty()){
   return Resp.fail("400","文件為空!");
  }
  //文件不為空的情況
  //獲得原始文件名(前端傳過(guò)來(lái)的文件名) 帶有拓展名
  //原始文件名存在一定問(wèn)題
  String OriginalFilename=file.getOriginalFilename();
  //根據(jù) 時(shí)間戳+拓展名=服務(wù)器文件名
  // 確定服務(wù)器文件名(經(jīng)過(guò)字符操作加上拓展名)
  String fileName= System.currentTimeMillis()+"."+OriginalFilename.substring(OriginalFilename.lastIndexOf(".")+1);
  //控制臺(tái)查看服務(wù)器文件名
  System.out.println(fileName);
  //確定文件儲(chǔ)存位置
  // 文件保存路徑 注意最后加上雙反斜杠 轉(zhuǎn)義字符所有雙反斜杠
  String filePath="F:\\Test\\";
  //目標(biāo)文件路徑 (實(shí)際創(chuàng)建在硬盤(pán)的文件)
  File dest=new File(filePath+fileName);
  //判斷dest的父目錄是否存在
  if(dest.getParentFile().exists())
   dest.getParentFile().mkdirs();

  try {
    //前端傳過(guò)來(lái)的文件拷貝在本地
    file.transferTo(dest);
   }catch (Exception e){
    e.printStackTrace();
    return Resp.fail("500",OriginalFilename+"上傳失??!");

   }
   //上傳成功 返回前端穿過(guò)來(lái)的文件名
   return Resp.success(fileName);


 }
}

第四步:postman測(cè)試上傳

可以看到文件以及成功上傳到本地啦!

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

惠来县| 福泉市| 孝昌县| 乐安县| 宜州市| 绥宁县| 平罗县| 古交市| 香河县| 剑川县| 六盘水市| 黄石市| 西华县| 鸡泽县| 左云县| 常宁市| 灵石县| 建德市| 巧家县| 安吉县| 隆德县| 萨迦县| 汾阳市| 福安市| 广南县| 沅江市| 巫山县| 和田县| 阿坝县| 龙里县| 湘乡市| 闵行区| 丽江市| 平湖市| 故城县| 永仁县| 宣恩县| 尉氏县| 克东县| 大石桥市| 宜州市|