springboot上传文件(存入服务器,并将URL存入数据库表中)「建议收藏」

springboot上传文件(存入服务器,并将URL存入数据库表中)「建议收藏」publicRupLoadAccessory(@RequestParam(“file”)MultipartFilefile){Map<String,Object>map=newHashMap<>();if(file.isEmpty()){map.put("result","fail&

大家好,又见面了,我是你们的朋友全栈君。

public R upLoadAccessory(@RequestParam("file")MultipartFile file){
		
		Map<String,Object> map = new HashMap<>();
		
		if (file.isEmpty()) {
			return R.ok(map);
			
		}else {
			
			//保存时的文件名
			DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); 
			Calendar calendar = Calendar.getInstance(); 
			String dateName = df.format(calendar.getTime())+file.getOriginalFilename();
			
			System.out.println(dateName);
			//保存文件的绝对路径
			WebApplicationContext webApplicationContext = (WebApplicationContext)SpringContextUtils.applicationContext; 
			ServletContext servletContext = webApplicationContext.getServletContext();
			String realPath = servletContext.getRealPath("/");
			String filePath = realPath + "WEB-INF"+File.separator + "classes" + File.separator +"static" + File.separator + "resource" + File.separator+dateName;
			System.out.println("绝对路径:"+filePath);
			
			File newFile = new File(filePath);
			
			//MultipartFile的方法直接写文件
			try {
				
				//上传文件
				file.transferTo(newFile);
				
				//数据库存储的相对路径
				String projectPath = servletContext.getContextPath();
				HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
				String contextpath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+projectPath;
				String url = contextpath + "/resource/"+dateName;
				System.out.println("相对路径:"+url);
				//文件名与文件URL存入数据库表
				....
				
				
			} catch (IllegalStateException | IOException e) {
				e.printStackTrace();
			}
			
		}
		return R.ok(map);
	}

2018.5.16 更新: 补充一下工具类

@Component
public class SpringContextUtils implements ApplicationContextAware {
	public static ApplicationContext applicationContext; 

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		SpringContextUtils.applicationContext = applicationContext;
	}

	public static Object getBean(String name) {
		return applicationContext.getBean(name);
	}

	public static <T> T getBean(String name, Class<T> requiredType) {
		return applicationContext.getBean(name, requiredType);
	}

	public static boolean containsBean(String name) {
		return applicationContext.containsBean(name);
	}

	public static boolean isSingleton(String name) {
		return applicationContext.isSingleton(name);
	}

	public static Class<? extends Object> getType(String name) {
		return applicationContext.getType(name);
	}

}

2019.8.6更新— HttpContextUtils 工具类

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class HttpContextUtils {

	public static HttpServletRequest getHttpServletRequest() {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
	}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/134632.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 卡巴斯基2月病毒及恶意软件排行榜

    卡巴斯基2月病毒及恶意软件排行榜互联网“热门”木马病毒排名下面显示了在2009年2月期间,中国地区的互联网上木马病毒的活跃情况。表中所列的都是最常遇到的恶意程序。这些恶意程序会在用户上网的同时给用户带来危害。(下表中的数据根据卡巴斯基产品检测情况统计得出)Nameofmalware % pp1. not-a-virus:AdWare.Win32.BHO.fay     7.69 +102. HEUR:Trojan…

    2022年7月25日
    15
  • Eclipse断点调试

    Eclipse断点调试作为开发人员,掌握开发环境下的调试技巧十分有必要。去年就想把关于Eclipse断点调试总结下了,由于对时间的掌控程度仍需极大提高,结果拖到今年才写了此篇博文。关于java调试技术还有很多,如JavaDebugInterface等,依据具体项目的需要,还有很多值得去研究和学习的。该博文仅就Eclipse断点调试技巧做下总结,不足够的地方还请大牛们指点。1 Debug视图1.1线程堆栈

    2022年5月21日
    95
  • 手游服务端架设一条龙_vm服务器

    手游服务端架设一条龙_vm服务器本文作者:smeli(俄罗斯人,于2009年完成该教程)PS:要比国内写的那些教程完整,详细,希望大家喜欢VS运行库安装………………………………………..2SQL数据库安装…………………………………………..3L2Server设置………………………………………………11GM账户创建………………………………………….15运行服务端(1)…

    2025年10月3日
    4
  • mac phpstorm 2021.4.14 激活码_通用破解码

    mac phpstorm 2021.4.14 激活码_通用破解码,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月15日
    59
  • static函数局部变量的使用「建议收藏」

    static函数局部变量的使用「建议收藏」#include&lt;stdio.h&gt;#include&lt;stdint.h&gt;#include&lt;string.h&gt;#include&lt;math.h&gt;#include&lt;stdlib.h&gt;voidstaticFun(void){  static uint8_t data=0;  data++;  data…

    2022年7月16日
    16
  • C语言 strstr函数的用法及模拟实现strstr函数「建议收藏」

    C语言 strstr函数的用法及模拟实现strstr函数「建议收藏」C语言strstr函数的用法及模拟实现strstr函数一、strstr函数的用法二、模拟实现strstr函数的功能一、strstr函数的用法1.strstr函数原型:char*strstr(constchar*str1,constchar*str2)2.功能:strstr()是一个参数为两个字符指针类型,返回值是char*类型的函数,它用于找到子串(str2)在一个字符串(str1)中第一次出现的位置。这里因为传进来的地址指向的内容不会在发生改变,所以我们在两个形参(char*)前加上c

    2022年6月25日
    47

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号