ssm上传文件获取路径_SSM-文件上传

因为开发环境和线上环境系统不一样,所以需要区别环境

config.java

可以判断系统进行自动化的区别,我是手动去切换注释的

public class config {

//public static final String filePath = "E:\\nginx-1.16.1\\html\\uploadFile";//本地

public static final String filePath = "/usr/local/nginx/html/uploadFile/"; //线上

//public static final String fileUrl = "http://localhost/uploadFile/"; //本地

public static final String fileUrl = "http://xx.xx.xx.xx/uploadFile/"; //线上

}

工具

// util/uuid.java

public class uuid {

public static String getUuid(){

String uuid = UUID.randomUUID().toString();

//去掉“-”符号

return uuid.replaceAll("-", "");

}

}

上传文件

// util/UploadFile.java

@Controller

public class UploadFile {

@RequestMapping("/uploadFile")

@ResponseBody

public Map upload(@RequestParam("file") MultipartFile file, HttpServletRequest req) throws Exception, IOException {

return saveFile(file, req);

}

private Map saveFile(MultipartFile file, HttpServletRequest req) {

Map map = new HashMap();

// 判断文件是否为空

if (!file.isEmpty()) {

try {

String fileId = uuid.getUuid();

String fileName = file.getOriginalFilename();//1.jpg 获取上传文件的全名加后缀

map.put("fileId",fileId);

map.put("fileName",fileName);

//这是指定的绝对路径

String filePath = config.filePath;

String fileUrl = config.fileUrl;

File realPathFolder = new File(filePath);

if (!realPathFolder.exists()) {

realPathFolder.mkdir();

}

String newName = fileId + "_" + fileName;//创建一个新的文件名称 FilenameUtils.getExtension(name):获取文件后缀名

File f = new File(realPathFolder,newName);

map.put("newName", newName);

map.put("fileUrl",fileUrl);

map.put("url",fileUrl+newName);

map.put("code", 20000); //成功的固定字段

map.put("msg", "上传file成功");

file.transferTo(f);//将上传的文件存储到指定位置

} catch (Exception e) {

map.put("code", 0);

map.put("msg", "上传file失败");

e.printStackTrace();

}

} else {

map.put("msg", "上传file失败");

}

return map;

}

}

上传base64

// util/UpLoadBase64.java

@Controller

public class UpLoadBase64 {

@RequestMapping(value = "/uploadBase64", method = RequestMethod.POST)

@ResponseBody

public Map base64UpLoad(@RequestParam String file,@RequestParam String fileName,HttpServletRequest req, HttpServletResponse res) {

Map map = new HashMap();

try {

String dataPrix = "";

String data = "";

if (file == null || "".equals(file)) {

throw new Exception("上传数据为空");

} else {

String[] d = file.split("base64,"); //把base, 切换

if (d != null && d.length == 2) {

dataPrix = d[0];

data = d[1];

} else {

throw new Exception("上传失败,数据不合法");

}

}

String fileId = uuid.getUuid();

map.put("fileId", fileId);

map.put("fileName", fileName);

//这是指定的绝对路径

String filePath = config.filePath;

String fileUrl = config.fileUrl;

//因为BASE64Decoder的jar问题,此处使用spring框架提供的工具包

byte[] bs = Base64Utils.decodeFromString(data);

File realPathFolder = new File(filePath);

if (!realPathFolder.exists()) {

realPathFolder.mkdir();

}

String newName = fileId + '_' + fileName;//创建一个新的文件名称.后缀名

FileUtils.writeByteArrayToFile(new File(realPathFolder,newName), bs);

map.put("newName", newName);

map.put("fileUrl",fileUrl);

map.put("url",fileUrl+newName);

map.put("code", 20000); //成功的固定字段

map.put("msg", "上传base64成功");

} catch (Exception e) {

map.put("msg", "上传base64失败");

e.printStackTrace();

}

return map;

}

}

删除文件

// util/DeleteFile.java

@Controller

public class DeleteFile {

@RequestMapping("/deleteFile")

@ResponseBody //不写会默认返回当前路径!!

public Map deleteFile(@RequestParam("fileName") String fileName) throws Exception, IOException {

Map map = new HashMap();

File file = new File(filePath,fileName);

if(file.exists()){

file.delete();

map.put("code",20000);

map.put("msg","文件删除成功");

}else{

map.put("msg","文件不存在");

}

return map;

}

}

文件下载

// util/DownLoad.java

@Controller

public class DownLoad {

/*

* 下载方式一:

* ①获取前台要下载的文件名称

* ②设置响应类型

* ③设置下载页显示的文件名

* ④获取下载文件夹的绝对路径和文件名合并为File类型

* ⑤将文件复制到浏览器

*/

@RequestMapping("/download")

@ResponseBody

public void download(HttpServletRequest req, HttpServletResponse resp, String fileName) throws Exception {

String filePath = config.filePath;

//String realPath = req.getServletContext().getRealPath("/download");//获取下载文件的路径

File file = new File(filePath,fileName);//把下载文件构成一个文件处理 filename:前台传过来的文件名称

//设置响应类型 ==》 告诉浏览器当前是下载操作,我要下载东西

resp.setContentType("application/x-msdownload");

//设置下载时文件的显示类型(即文件名称-后缀) ex:txt为文本类型

resp.setHeader("Content-Disposition", "attachment;filename=" + fileName);

//下载文件:将一个路径下的文件数据转到一个输出流中,也就是把服务器文件通过流写(复制)到浏览器端

Files.copy(file.toPath(), resp.getOutputStream());//Files.copy(要下载的文件的路径,响应的输出流)

}

}

这些代码随便百度就有,还有上传多个文件的,自行百度

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值