文件 / 图片 上传通用版

 HTML和JS官网有:https://www.layui.com/demo/upload.html

java函数可以用通用上传方式,我写了个文件记录表,所以写了个通用的,文件图片都可以,JS限制就可以了。

/**
 * 文件上传下载 操作处理
 *
 * @author admin
 * @date Mon Oct 07 09:45:29 CST 2019
 */
@Controller
@RequestMapping("/system/webFiles")
public class UploadDownloadController extends BaseController {
    private String prefix = "system/webFiles";

    @Autowired
    private IWebPubFilesService webPubFilesService;

    private static final Logger log = LoggerFactory.getLogger(CommonController.class);

    /**
     * 单个文件上传
     */
    @PostMapping(value = "/upload")
    @ResponseBody
    public AjaxJson upload(@RequestParam("file") MultipartFile file, HttpServletRequest request,
                           HttpServletResponse response) throws IOException {
        AjaxJson res = new AjaxJson();
        FileUPMessage msg = new FileUPMessage();
        if (!file.isEmpty()) {
            //原文件名
            String fileName = file.getOriginalFilename();
            //后缀名
            String suffix = fileName.substring
                    (fileName.lastIndexOf(".") + 1).toUpperCase();
            //后缀名
            String suffi = fileName.substring
                    (0,fileName.lastIndexOf(".")).toUpperCase();
            boolean isUpload = false;
            boolean isImg = false;
            //检查文件合法性 是否包含在UPFILESuffix中
            for (UpFileSuffix suffixV : UpFileSuffix.values()) {
                String str = suffixV.name();
                if (suffix.equals(str)) {
                    isUpload = true;
                    break;
                }
            }
            if (isUpload == false) {
                res.setSuccess(false);
                msg.setError("不是有效的文件" + fileName);
                res.put("msg", msg);
                return res;
            }
            //判断是否为图片
            for (ImgFileSuffix suffixIMG : ImgFileSuffix.values
                    ()) {
                String str = suffixIMG.name();
                if (suffix.equals(str)) {
                    isImg = true;
                    break;
                }
            }
            String MID = request.getParameter("mid");
            if (MID == null || MID.length() == 0) {
                MID = "0";
            }
            int mid = Integer.valueOf(MID);
            String model = request.getParameter("model");
            if (model == null || model.length() == 0) {
                model = "DEFAULT";
            }
            String rootPath = Global.getUserfilesBaseDir() +
                    Global.USERFILES_BASE_URL;
            String rootPathThumb = Global.getUserfilesBaseDir() +
                    Global.USERFILES_BASE_URL + "ThumbDEFAULT" + File.separator;
            String MOSOFILES_BASE_URL = rootPath;
            InputStream in = null;
            OutputStream out = null;
            try {
                String path = MOSOFILES_BASE_URL + "webupload" +
                        File.separator + model + File.separator;
                String pathThumb = rootPathThumb + "webupload" +
                        File.separator + model + File.separator;
                String FileURL = Global.USERFILES_BASE_URL +
                        "webupload/" + model + "/";

                System.out.println(path);
                System.out.println(FileURL);
                File dir = new File(path);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                File dirThumb = new File(pathThumb);
                if (!dirThumb.exists()) {
                    dirThumb.mkdirs();
                }
                /*String uuid = UUID.randomUUID().toString
                        ().replace("-", "").toLowerCase();*/
                //新文件名
                String newFilename = suffi+"-"+ mid + "." + suffix;
                //新文件全地址
                File serverFile = new File(dir.getAbsolutePath()
                        + File.separator + newFilename);
                //压缩新文件全地址
                String serverFileThumb =
                        dirThumb.getAbsolutePath()
                                + File.separator + newFilename;

                //创建文件记录对象
                WebPubFiles webpubFiles = new WebPubFiles();
                webpubFiles.setBeginCreateDate(new Date());
                in = file.getInputStrea
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值