springboot文件上传mysql_springboot+vue实现文件上传

技术:

后端:springboot

前端框架:vue

数据库:mysql

pom.xml:

commons-fileupload

commons-fileupload

1.3.3

commons-io

commons-io

2.4

controller:

@RestController

@RequestMapping("/yfjs")

@CrossOrigin

public class YFJSController {

@Autowired

private YFJSService yfjsService;

@Autowired

private FJSCService fjscService;

private String url;

@RequestMapping(value="/file",produces="application/json;charset=UTF-8")

@ResponseBody

public JSONObject uploadFile(@RequestParam("file") MultipartFile file,@RequestParam("yundanhao")String yundanhao,@RequestParam("jiyunxiang_Id")String jiyunxiang_Id) {

JSONObject rst = new JSONObject();

rst.put("yundanhao",yundanhao);

rst.put("id",jiyunxiang_Id);

System.out.print("上传文件==="+"\n");

//判断文件是否为空

if (file.isEmpty()) {

rst.put("msg","文件为空");

return rst;

}

// 获取文件名

String fileName = file.getOriginalFilename();

// System.out.print("上传的文件名为: "+fileName+"\n");

fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + "_" + fileName;

System.out.print("(加个时间戳,尽量避免文件名称重复)保存的文件名为: "+fileName+"\n");

//加个时间戳,尽量避免文件名称重复

//String path = "/opt/apache-tomcat-8.0.50/webapps/img/" +fileName;

//String path = "/opt/apache-tomcat-8.0.50/webapps/img/" +fileName;

String path = "/opt/tomcat/apache-tomcat-8.0.50/webapps/img/" +fileName;

//String path = "E:/fileUpload/" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + "_" + fileName;

//文件绝对路径

System.out.print("保存文件绝对路径"+path+"\n");

//创建文件路径

File dest = new File(path);

//判断文件是否已经存在

if (dest.exists()) {

rst.put("msg","文件已经存在");

return rst;

}

try {

//上传文件

file.transferTo(dest); //保存文件

System.out.print("保存文件路径"+path+"\n");

//url="http://你自己的域名/项目名/images/"+fileName;//正式项目

url="http://测试服务器:tomcat端口号/img/"+fileName;//本地运行项目

//url="http://正式服务器2:tomcat端口号/i

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值