写点什么

Bootsrap FileInput 图片上传以及配置虚拟路径(简单易懂),java 分布式框架技术方案

作者:MySQL神话
  • 2021 年 11 月 26 日
  • 本文字数:3712 字

    阅读完需:约 12 分钟

$(".myfile").fileinput({


uploadUrl:"../upload/file_upload", //接受请求地址


uploadAsync : true, //默认异步上传


showUpload : false, //是否显示上传按钮,跟随文本框的那个


showRemove : false, //显示移除按钮,跟随文本框的那个


showCaption : true,//是否显示标题,就是那个文本框


showPreview : true, //是否显示预览,不写默认为 true


dropZoneEnabled : false,//是否显示拖拽区域,默认不写为 true,但是会占用很大区域


//minImageWidth: 50, //图片的最小宽度


//minImageHeight: 50,//图片的最小高度


//maxImageWidth: 500,//图片的最大宽度


//maxImageHeight: 500,//图片的最大高度


//maxFileSize: 0,//单位为 kb,如果为 0 表示不限制文件大小


maxFileCount : 1, //表示允许同时上传的最大文件个数


enctype : 'multipart/form-data',


validateInitialCount : true,


previewFileIcon : "<i class='glyphicon glyphicon-king'></i>",


msgFilesTooMany : "选择上传的文件数量({n}) 超过允许的最大数值{m}!",


allowedFileTypes : [ 'image' ],//配置允许文件上传的类型


allowedPreviewTypes : [ 'image' ],//配置所有的被预览文件类型


allowedPreviewMimeTypes : [ 'jpg', 'png', 'gif' ],//控制被预览的所有 mime 类型


language : 'zh'


})


//异步上传返回结果处理


$('.myfile').on('fileerror', function(event, data, msg) {


console.log("fileerror");


console.log(data);


});


//异步上传返回结果处理


$(".myfile").on("fileuploaded", function(event, data, previewId, index) {


console.log("获取路径地址为:"+data.response.url)


var urls = data.response.url;


$("#url").val(urls);


$("#url1").val(urls);


});


//上传前


$('.myfile').on('filepreupload', function(event, data, previewId, index) {


console.log("filepreupload");


});


前端页面就这点东西,没了都写出来!!然后说一下后端上传问题


后端上传代码




天知道我写改这个玩意改了多久!!!愁死我了!!连包都给你贴出来!!多贴心啊!!我把多余的代码都删掉了,可以直接使用,改改路径就行。。额。。连路径都不需要改!



import com.fasterxml.jackson.databind.ObjectMapper;


import com.google.common.io.ByteStreams;


import org.apache.tomcat.util.http.fileupload.FileItemFactory;


import org.apache.tomcat.util.http.fileupload.FileUploadException;


import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;


import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;


import org.springframework.stereotype.Controller;


import org.springframework.util.ResourceUtils;


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


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


import org.springframework.web.multipart.MultipartFile;


import org.springframework.web.multipart.MultipartHttpServletRequest;


import javax.servlet.ServletContext;


import javax.servlet.ServletException;


import javax.servlet.http.HttpServletRequest;


import javax.servlet.http.HttpServletResponse;


import java.io.File;


import java.io.FileNotFoundException;


import java.io.FileOutputStream;


import java.io.IOException;


import java.nio.charset.Charset;


import java.text.SimpleDateFormat;


import java.util.*;


/**


  • @ClassName:FileUploadController

  • @Description:TODO 文件上传

  • @Author LinLuoChen

  • @Date 2019/7/29/10:27

  • @Version V1.0


**/


@Controller


@RequestMapping("/upload")


public class FileUploadController {


private static final ObjectMapper objectMapper = new ObjectMapper();


@RequestMapping(value="/file_upload")


@ResponseBody


public Map<String, Object> fileUpload(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException,


FileUploadException {


ServletContext application = request.getSession().getServletContext();


String savePath = "C:/upload/";


// 文件保存目录 URL


String saveUrl = request.getContextPath() + "/upload/";


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


HashMap<String, String> extMap = new HashMap<String, String>();


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 = 1000000;


response.setContentType("text/html; charset=UTF-8");


if (!ServletFileUpload.isMultipartContent(request)) {


return getError("请选择文件。");


}


// 检查目录


File uploadDir = new File(savePath);


if (!uploadDir.isDirectory()) {


uploadDir.mkdir();


// return getError("上传目录不存在。");


}


// 检查目录写权限


if (!uploadDir.canWrite()) {


return getError("上传目录没有写权限。");


}


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


if (dirName == null) {


dirName = "image";


}


if (!extMap.containsKey(dirName)) {


return getError("目录名不正确。");


}


// 创建文件夹


savePath += dirName + "/";


saveUrl += dirName + "/";


File saveDirFile = new File(savePath);


if (!saveDirFile.exists()) {


saveDirFile.mkdirs();


}


SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");


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


savePath += ymd + "/";


saveUrl += ymd + "/";


File dirFile = new File(savePath);


if (!dirFile.exists()) {


dirFile.mkdirs();


}


FileItemFactory factory = new DiskFileItemFactory();


ServletFileUpload upload = new ServletFileUpload(factory);


upload.setHeaderEncoding("UTF-8");


MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;


Iterator item = multipartRequest.getFileNames();


while (item.hasNext()) {


String fileName = (String) item.next();


MultipartFile file = multipartRequest.getFile(fileName);


// 检查文件大小


if (file.getSize() > maxSize) {


return getError("上传文件大小超过限制。");


}


// 检查扩展名


String fileExt = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1).toLowerCase();


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


return getError("上传文件扩展名是不允许的扩展名。\n 只允许"


  • extMap.get(dirName) + "格式。");


}


SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");


String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;


try {


File uploadedFile = new File(savePath, newFileName);


ByteStreams.copy(file.getInputStream(), new FileOutputStream(uploadedFile));


} catch (Exception e) {


return getError("上传文件失败。");


}


Map<String, Object> msg = new HashMap<String, Object>();


msg.put("success", 0);


msg.put("url", saveUrl + newFileName);


return msg;


}


return null;


}


private Map<String, Object> getError(String message) {


Map<String, Object> msg = new HashMap<String, Object>();


msg.put("error", 1);


msg.put("message", message);


return msg;


}


class NameComparator implements Comparator {


public int compare(Object a, Object b) {


Hashtable hashA = (Hashtable) a;


Hashtable hashB = (Hashtable) b;


if (((Boolean) hashA.get("is_dir"))


&& !((Boolean) hashB.get("is_dir"))) {


return -1;


} else if (!((Boolean) hashA.get("is_dir"))


&& ((Boolean) hashB.get("is_dir"))) {


return 1;


} else {


return ((String) hashA.get("filename"))


.compareTo((String) hashB.get("filename"));


}


}


}


class SizeComparator implements Comparator {


public int compare(Object a, Object b) {


Hashtable hashA = (Hashtable) a;


Hashtable hashB = (Hashtable) b;


if (((Boolean) hashA.get("is_dir"))


&& !((Boolean) hashB.get("is_dir"))) {


return -1;

Kafka 实战笔记

关于这份笔记,为了不影响大家的阅读体验,我只能在文章中展示部分的章节内容和核心截图


![image.png](https://upload-images.jianshu.io/u


《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享


pload_images/24616006-873dd5cc89b3be85.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


  • Kafka 入门

  • 为什么选择 Kafka

  • Karka 的安装、管理和配置



  • Kafka 的集群

  • 第一个 Kafka 程序


afka 的生产者



  • Kafka 的消费者

  • 深入理解 Kafka

  • 可靠的数据传递




  • Spring 和 Kalka 的整合

  • Sprinboot 和 Kafka 的整合

  • Kafka 实战之削峰填谷

  • 数据管道和流式处理(了解即可)



  • Kafka 实战之削峰填谷



本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

用户头像

MySQL神话

关注

还未添加个人签名 2021.11.12 加入

还未添加个人简介

评论

发布
暂无评论
Bootsrap FileInput 图片上传以及配置虚拟路径(简单易懂),java分布式框架技术方案