springMVC 获取本地项目路径 及后整理上传文件的方法

String path=request.getSession().getServletContext().getRealPath("upload/img/product");

//二进制上传
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
//获取文件
CommonsMultipartFile fpic=(CommonsMultipartFile) multipartRequest.getFile("fpic");
//推断是否有文件提交
if(fpic!=null){
if(!fpic.isEmpty()){
    String time=DataUtil.getCurrentTimeMillis();
    String path=request.getSession().getServletContext().getRealPath("upload/img/product");
    File file = new File(path+"/"+time+ ".jpg");
    fpicurl = "upload/img/product/p"+time+".jpg";
    try {
         fpic.getFileItem().write(file);
    } catch (Exception e) {
         e.printStackTrace();
         return;
    }
}
}



须要工具包

import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.File;
import java.io.IOException;

import com.realgo.util.DataUtil;


class DataUtil

	public static String getCurrentTimeMillis(){
		return String.valueOf(System.currentTimeMillis());
	}




版权声明:本文博客原创文章,博客,未经同意,不得转载。

posted @   mengfanrong  阅读(12338)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2014-08-18 关于window.history.back()后退问题
2014-08-18 区间Dp 暴力枚举+动态规划 Hdu1081
2014-08-18 MongoDB入门简单介绍
2014-08-18 Windows8和MacOS10.9双系统安装及Mac经常使用软件安装--联想E49A
2014-08-18 R语言学习笔记
2014-08-18 C++0x简讯
2014-08-18 fscanf()函数具体解释
点击右上角即可分享
微信分享提示