Java传输文件面板_关于kindeitor java后端封装上传的jsp文件

请求方面:

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

public String upload(MultipartHttpServletRequest request, HttpServletResponse response){

String s = fileUpdateUtil.fileUpload(request, response);

try {

String referer = request.getHeader("referer");

Pattern p = Pattern.compile("([a-z]*:(//[^/?#]+)?)?", Pattern.CASE_INSENSITIVE);

Matcher mathcer = p.matcher(referer);

if (mathcer.find()) {

String callBackPath = mathcer.group();// 请求来源a.com

MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

MultipartFile imgFile = multipartRequest.getFile("fileupload");//k4中fileupload

//JsonObject json =自己的处理imgFile

// 同域时直接返回json.toString()即可无需redirect

String url = "redirect:" + callBackPath + "/redirect?s=" + s + "#" + s;

return url;

} else {

System.out.println("upload referer not find");

}

} catch (Exception e) {

System.out.println("upload error"+e);

}

return null;

}

工具类:

public static String fileUpload(MultipartHttpServletRequest request, HttpServletResponse response){

String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();//获取项目绝对路径

String substring = rootPath.substring(1, rootPath.length()).replace("/", "\\");

String savePath = substring + "static\\attached\\";

// 文件保存目录URL

// 定义允许上传的文件扩展名

HashMap extMap = new HashMap();

extMap.put("image", "gif,jpg,jpeg,png,bmp");

extMap.put("flash", "swf,flv");

extMap.put("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");

extMap.put("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");

// 最大文件大小

long maxSize = 100000000;

response.reset();

response.setHeader("P3P","CP=CAO PSA OUR");

response.setCharacterEncoding("UTF-8");

response.setContentType("text/html");

// writer.println(json); //想办法把map转成json

if (!ServletFileUpload.isMultipartContent(request)) {

System.out.println("请选择文件。");

return null;

}

File uploadDir = new File(savePath);

if (!uploadDir.exists()) {

uploadDir.mkdirs();

}

// 检查目录写权限

if (!uploadDir.canWrite()) {

System.out.println("上传目录没有写权限。");

return null;

}

String dirName = request.getParameter("dir");

if (dirName == null) {

dirName = "image";

}

if (!extMap.containsKey(dirName)) {

System.out.println("目录名不正确。");

return null;

}

// 创建文件夹

savePath += dirName + "/";

File saveDirFile = new File(savePath);

if (!saveDirFile.exists()) {

saveDirFile.mkdirs();

}

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

String ymd = sdf.format(new Date());

savePath += ymd + "/";

File dirFile = new File(savePath);

if (!dirFile.exists()) {

dirFile.mkdirs();

}

List files = request.getFiles("imgFile");

Iterator itr = files.iterator();

while (itr.hasNext()) {

MultipartFile item = itr.next();

String fileName = item.getOriginalFilename();

// 检查文件大小

if (item.getSize() > maxSize) {

System.out.println("上传文件大小超过限制。");

return null;

}

// 检查扩展名

String fileExt = fileName.substring(

fileName.lastIndexOf(".") + 1).toLowerCase();

if (!Arrays.asList(extMap.get(dirName).split(","))

.contains(fileExt)) {

System.out.println("上传文件扩展名是不允许的扩展名。只允许" + extMap.get(dirName) + "格式。");

return null;

}

SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");

String newFileName = df.format(new Date()) + "_"

+ new Random().nextInt(1000) + "." + fileExt;

try {

File uploadedFile = new File(savePath, newFileName);

item.transferTo(uploadedFile);

} catch (Exception e) {

System.out.println("上传文件失败");

}

//http://192.168.100.157:8080/attached/image/20190923/20190923094646_10.png

int fast = savePath.indexOf("static")+6;

String savePatha = savePath.substring(fast, savePath.length());

String replace = savePatha.replace("\\", "/");

String path = "http://192.168.100.157:8080"+replace + newFileName;

String url = "{\"error\":0,\"url\":\""+path+"\"}";

return url;

}

return null;

}

都是整合别的博主的,还有其他的一些,后面补充

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值