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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python中的递归问题,求圆周率

    python中的递归问题,求圆周率

    2021年11月19日
    43
  • idea2021永久激活码【2021.8最新】「建议收藏」

    (idea2021永久激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月26日
    195
  • 电信光猫tewa-708e超级密码_天翼网关hg6201t超级密码

    电信光猫tewa-708e超级密码_天翼网关hg6201t超级密码文章转载:玩转盒子前些年各地运营商响应国家政策,光进铜退小区宽带由网线入户改成光缆入户,这样必须用光猫把光信号转换成电信号。我用的是中国电信的宽带,2017年我们小区也进行了网络改造,网线入户变成了光缆入户。电信给配的光猫型号是TEWA-600NGM,我这个光猫只有一个千兆网口,一个百兆网口和一个USB口,没有电话插口。这个光猫自带无线WiFi,WiFi名称是ChinaNet-XXXX(不同…

    2022年10月8日
    0
  • Java中用fastjson对String、JSONObject、JSONArray相互转换

    Java中用fastjson对String、JSONObject、JSONArray相互转换fastjson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将JavaBean序列化为JSON字符串,也可以从JSON字符串反序列化到JavaBean下面主要是本人在工作中经常用到的关于String、JSONObject、JSONArray的相互装换String——&gt;&gt;&gt;JSONArrayStringst="[{name:Tim,age…

    2022年6月20日
    20
  • centos系统不识别u盘_Unable to locate package vim

    centos系统不识别u盘_Unable to locate package vim在修改源后使用sudoapt-getupdate 显示 W:GPGerror:https://mirrors.ustc.edu.cn/kalikali-rollingInRelease:Thefollowingsignaturescouldn’tbeverifiedbecausethepublickeyisnotavailable:…

    2022年10月13日
    0
  • JSP页面通过URL传递中文参数出现中文乱码问题

    JSP页面通过URL传递中文参数出现中文乱码问题做项目时遇到这个问题问题描述:通过点击这个参数名并进行跳转control层,进行参数接受并处理。${cs.name}里面包含中文&lt;a href="${pageContext.request.contextPath}/recommend_navCategory?name=${cs.name}"&gt;${cs.name}&lt;/a&gt;服务器端:ISO-8859-1是tomca…

    2022年6月13日
    29

发表回复

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

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