首页 > 其他分享 >文件上传-code

文件上传-code

时间:2023-11-10 16:15:06浏览次数:34  
标签:文件 code String realPath File new 上传

     1.导入上传文件gav坐标

       

  <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
   <dependency>
   <groupId>commons-fileupload</groupId>
   <artifactId>commons-fileupload</artifactId>
   <version>1.4</version>
  </dependency>

  

2.上传到服务器下

@RequestMapping(value = "/upload",method = RequestMethod.POST)
public String upload(MultipartFile multipartFile, HttpServletRequest request) throws IOException {
String uuid = UUID.randomUUID().toString().replace("-", "");
//拿到文件的原始名
String name = multipartFile.getOriginalFilename();
int index = name.lastIndexOf("."); //拿到文件最后
String newFileName = uuid + "." + name.substring(index + 1);//拿到文件名的后缀
//long l = System.currentTimeMillis(); 时间戳
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
String format = simpleDateFormat.format(new Date());
String realPath = request.getServletContext().getRealPath("/upload/"+ format);
System.out.println(realPath);
File file = new File(realPath);
// 判断当前上传路径是否存在
if (!file.exists()) {
file.mkdirs();
}
//实现文件长传
File file1 = new File(realPath+File.separator+newFileName);
multipartFile.transferTo(file1);
return "success";

}

标签:文件,code,String,realPath,File,new,上传
From: https://www.cnblogs.com/fangyilovebc/p/17824332.html

相关文章

  • rpm包制作脚本spec文件编写详细指南
    概述spec文件是制作rpm包的脚本文件,详细定义rpm包的信息、包含内容和安装位置,如软件包的名字、版本、类别、说明摘要、创建时要执行什么指令、安装时要执行什么操作、以及软件包所要包含的文件列表等等。spec文件有多个段组成,分别定义rpm编译、打包、安装等阶段的工作内容。示例如......
  • linux diff求两个文件的差集
    awk从文本中过滤出需要的ipqueryId_20231109214653_ipBatchQueryResult.json{"id":0,"ip":"121.204.216.130","type":1,"domain":"","agreement":"","mode":"","postalCo......
  • php批量上传大文件,php实现批量上传图片的技巧
    我们在很多时候并不是上传一张图片就可以了需要同时上传N张图片,这个时候该怎么办了?我这里以前写了个例程大家看看,或许可以启发思路!php提交图片页面程序部分,可以实现动态显示图片的数量:enctype=”multipart/form-data”method=”post”name=”Form1″id=”Form1″>functiont......
  • python Compile failed: command '/usr/bin/clang' failed with exit code 1 解决办
    一、升级pippip3install--upgradepip然后,更新设置工具:python3-mpipinstall--upgradesetuptools......
  • 新建项目相关的css文件及设置
     global.css/*全局样式表*/html,body,#app{ height:100%; margin:0; padding:0;} 2.reset.css/***EricMeyer'sResetCSSv2.0(http://meyerweb.com/eric/tools/css/reset/)*http://cssreset.com*/html,body,div,span,applet,object,iframe,......
  • windows注册dll文件
    帮忙装了一个C端的程序,碰到了之前没有接触过的功能———注册dll文件。注册dll文件还是挺简单的。首先按住win+r大开运行窗口,然后输入regsvr32,把需要注册的dll文件拖到运行窗口中,此时就会把dll文件的路径填充到文本框中,格式类似下面这样:regsvr32D:\xxxx.dll点击确定即可......
  • Educational Codeforces Round 157 D
    tilian不太会这种题发现找到一个数就能确定整个序列然后转而发现前缀异或和b1^b2=a1b1^b3=a2...我们发现要是n为偶数时能直接求出b1从而确定整个序列而为奇数时我们无法确定b1我们思考拆位之后如果b1该位为0算出真实的异或后的01个数b1该位为1算出真实的......
  • Codeforces Round 908 (Div. 2)
    比赛链接A.SecretSport题解O(1*T)对于一场比赛,结束前谁最后赢就是谁赢#include<bits/stdc++.h>usingnamespacestd;strings;voidsolve(){intn;cin>>n>>s;cout<<s[n-1]<<endl;}intmain(){intT;cin>>T......
  • SprigMvc文件下载
    @RequestMapping(method=RequestMethod.GET,value="/down")publicResponseEntity<byte[]>DownLoad(HttpServletRequestrequest,Stringfilename){//获取文件的真实路径StringrealPath=request.getServletContext().getRealPath("/WEB-INF/d......
  • CentOS系统,文件压缩与解压命令大全
    在CentOS系统中,可以使用多种命令进行文件压缩和解压缩操作。以下是常见的文件压缩和解压命令及其用法:一、tar:用于打包文件或目录,并可选择压缩为tar压缩包1.创建tar压缩包:tar-cvfarchive.tarfile1file2directory2.解压他让压缩包:tar-xvfarchive.tar3.创建tar.gz压缩......