java服务器接收到文件返回提示,java实现文件上传功能(返回服务器文件路径)

有时候咱们会有一些需求须要上传一些图片或者一些文件,咱们写一个小例子来实现这个功能.

我此次是针对上传图片来作的,其实就是后缀名限制,返回本身定义

import java.io.File;

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.HashMap;

import java.util.Map;

import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

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

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

import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.multipart.MultipartFile;

@RestController

@RequestMapping("/company/public")

public class PublicController {

/**

* 上传用户反馈图片

* @param file

* @param request

* @param response

* @author sunran

* @return

*/

@ResponseBody

@RequestMapping("/upload")

public Object uploadPicture(@RequestParam(value="file",required=false)MultipartFile file,HttpServletRequest request,HttpServletResponse response){

ResultResp result = new ResultResp();

int maxSize = 1024*1024*1; //上传最大为1MB

if (file.getSize()>maxSize) {

result.setMsg("最大上传限制1Mb");

return result;

}

Map map = new HashMap();

File targetFile=null;

String url="";//返回存储路径

String fileName=file.getOriginalFilename();//获取文件名加后缀

if(fileName!=null&&fileName!=""){

String path = "/data/file/";

//String path = "D:/data/file/";

String fileF = fileName.substring(fileName.lastIndexOf("."), fileName.length());//文件后缀

if (!(fileF.equals(".jpg") || fileF.equals(".jpeg") || fileF.equals(".png") || fileF.equals(".image"))) {

result.setMsg("只能上传jpg,jpeg,png,image格式");

return result;

}

//新的文件名

fileName=new Date().getTime()+"_"+new Random().nextInt(1000)+fileF;

//获取文件夹路径

File file1=new File(path);

//若是文件夹不存在则建立

if(!file1.exists() && !file1.isDirectory()){

file1.mkdirs();

}

//将图片存入文件夹

targetFile = new File(file1, fileName);

try {

//将上传的文件写到服务器上指定的文件。

file.transferTo(targetFile);

//赋予权限

String command = "chmod 775 -R " + targetFile;

Runtime runtime = Runtime.getRuntime();

Process proc = runtime.exec(command);

//生成文件地址

url="http://XXXXXXXXX.cn"+path+"/"+fileName;

result.setCode(200+"");

result.setMsg("图片上传成功");

System.out.println("图片上传成功 url:"+url);

map.put("url", url);

result.setData(map);

} catch (Exception e) {

e.printStackTrace();

result.setMsg("系统异常,图片上传失败");

}

}

return result;

}

}

咱们能够本地测试一下,把路径改为咱们电脑本地就好了

d115b76c4405d35b7193c1376e87b4d3.png

58e1059dc195320a1f9080fecf9d83de.png

测试一下

615c84e2860c21dcb9d0a6247bbf4175.png

0bf46cf45baff702c8d8a659948f530a.png

718b0452f3684643957ae6ad.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值