17、【 商品管理模块开发】——后台商品图片的springmvc和富文本上传以及ftp文件服务器的开发

简介: 1、FTP文件服务器的搭建:软件下载:ftpserver;image.png浏览器访问:ftp://127.0.0.1/image.

1、FTP文件服务器的搭建:

软件下载:ftpserver

img_d3463456a5fabc2022fa8e071a9a0f5e.png
image.png

浏览器访问: ftp://127.0.0.1/

img_487995788bafbe8ba5bac37a5afe2a20.png
image.png

点击任意一个文件,就可以看到我们图片啦,前提是前面指定的目录里面有图片文件~


img_98521e030f50f36d68dfcd1987e1d393.png
image.png

2、接口编写:

1、springmvc方法上传文件:

ProductManageController:中编写下面方法:
*Controller:

   //springmvc文件上传接口
    @RequestMapping("upload.do")
    @ResponseBody
    public ServerResponse upload(HttpSession session, @RequestParam(value = "upload_file",required = false) MultipartFile file, HttpServletRequest request){
        User user=(User) session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"未登录,请先登录");
        }
        if(iUserService.checkAdminRole(user).isSuccess()){

            String path=request.getSession().getServletContext().getRealPath("upload");
            String targetFileName=iFileService.upload(file,path);
            String url= PropertiesUtil.getProperty("ftp.server.http.prefix")+targetFileName;

            Map fileMap= Maps.newHashMap();

            fileMap.put("uri",targetFileName);
            fileMap.put("url",url);
            return ServerResponse.createBySuccess(fileMap);

        }else {
            return ServerResponse.createByErrorMessage("当前登录者不是管理员,无权限操作");
        }
    }

这行代码需要注意的是@RequestParam(value = "upload_file",required = false) MultipartFile file参数的传入,对应的是相关文件类属性。

public ServerResponse upload(HttpSession session, @RequestParam(value = "upload_file",required = false) MultipartFile file, HttpServletRequest request)

*Service:

    //文件上传方法实现
    String upload(MultipartFile file, String path);

*ServiceImpl:

//文件上传方法实现
    public String upload(MultipartFile file,String path){
        String fileName=file.getOriginalFilename();
        //扩展名
        //abc.jpg 我们要拿到jpg
        String fileExtensionName=fileName.substring(fileName.lastIndexOf(".")+1);
        //防止文件被覆盖,我们使用UUID生产的字符串作为文件名,这样用户上传同名的文件就不会被覆盖了
        String uploadFileName= UUID.randomUUID().toString()+"."+fileExtensionName;
        logger.info("开始上传文件...上传文件的文件名:{},上传的路径:{},新文件名:{}",fileName,path,uploadFileName);

        //创建文件夹
        File fileDir=new File(path);
        if(!fileDir.exists()){
            fileDir.setWritable(true);
            fileDir.mkdirs();
        }
        //上传文件
        File targetFile=new File(path,uploadFileName);

        try {
            file.transferTo(targetFile);
            //文件上传成功

            //将targetFile上传到我们的文件服务器
            FTPUtil.uploadFile(Lists.newArrayList(targetFile));

            //文件已经上传到FTP服务器上
            //上传文件到文件服务器之后,删除我们Tomcat里面的文件,防止存储文件过多
            targetFile.delete();

        } catch (IOException e) {
            logger.error("上传文件异常",e);
            return null;
        }

        return targetFile.getName();
    }

由于是直接讲文件上传到文件服务器,所以不涉及到数据库的操作~

2、富文本上传:

富文本我们选择的是simditor

img_b636b22023db4a93925446b491c65fc5.png
image.png

相关文档位置:
https://simditor.tower.im/docs/doc-config.html#anchor-defaultImage

img_52c68d923c3f575ff0050693a8f27cb7.png
image.png
    //富文本上传接口
    @RequestMapping("richtext_img_upload.do")
    @ResponseBody
    public Map richtextImgUpload(HttpSession session, @RequestParam(value = "upload_file",required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse response){
        Map resultMap=Maps.newHashMap();

        User user=(User) session.getAttribute(Const.CURRENT_USER);
        if(user==null){

            resultMap.put("success",false);
            resultMap.put("msg","未登录,请先登录");
            return resultMap;
        }

        //富文本中对于返回值有自己的要求,我们使用是simditor 所以要按照simditor的要求进行返回

        if(iUserService.checkAdminRole(user).isSuccess()){

            String path=request.getSession().getServletContext().getRealPath("upload");
            String targetFileName=iFileService.upload(file,path);

            if(StringUtils.isBlank( targetFileName)){
                resultMap.put("success",false);
                resultMap.put("msg","上传失败");
                return  resultMap;
            }
            String url= PropertiesUtil.getProperty("ftp.server.http.prefix")+targetFileName;

            resultMap.put("success",true);
            resultMap.put("msg","上传成功");
            resultMap.put("ile_path",url);
            response.addHeader("Access-Control-Allow-Headers","X-File-Name");
            return resultMap;

        }else {
            resultMap.put("success",false);
            resultMap.put("msg","当前登录者不是管理员,无权限操作");
            return resultMap;
        }
    }

至于上传的upload方法我们还是使用springmvc中使用的方法~

3、测试接口:

接下来就是编写一个页面测试这两个方法啦
index.jsp页面中编写下面代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<html>
<body>
<h2>Hello World!</h2>

springmvc上传文件

<form name="form1" action="/manage/product/upload.do" method="post" enctype="multipart/form-data">
    <input type="file" name="upload_file">
    <input type="submit" value="springmvc上传文件">
</form>

富文本图片上传
<form name="form1" action="/manage/product/richtext_img_upload.do" method="post" enctype="multipart/form-data">
    <input type="file" name="upload_file">
    <input type="submit" value="富文本上传文件">
</form>
</body>
</html>

1、springmvc测试:
1


img_3c33363e97d1d260ca339a5537dfdb20.png
image.png

2


img_d81fe203f90a496c0614c864703f45c8.png

3


img_f6e6b285bdd24b830dbfba7f9c38d267.png
image.png

2、符文本测试:
1
img_7cec4bdc0dfe65e197f8a77bc52e82e8.png
image.png

2


img_16696ec0ac31cd5074779d6fdfc7b4c4.png

3
img_cb79ec811ab02d0329f658bb0cc4c74c.png
image.png
相关文章
|
3天前
|
前端开发 数据处理 API
后端开发:构建稳健与高效的服务器逻辑
后端开发:构建稳健与高效的服务器逻辑
|
2天前
|
JSON JavaScript API
使用 Node.js 开发一个简单的 web 服务器响应 HTTP post 请求
使用 Node.js 开发一个简单的 web 服务器响应 HTTP post 请求
9 1
|
2天前
|
JSON JavaScript 中间件
使用 Node.js 开发一个简单的 web 服务器响应 HTTP get 请求
使用 Node.js 开发一个简单的 web 服务器响应 HTTP get 请求
9 2
|
2天前
|
存储 前端开发 Linux
在 SAP ABAP 系统里访问 FTP 服务器
在 SAP ABAP 系统里访问 FTP 服务器
8 0
|
2天前
|
存储 JSON JavaScript
Node.js 上开发一个 HTTP 服务器,监听某个端口,接收 HTTP POST 请求并处理传入的数据
Node.js 上开发一个 HTTP 服务器,监听某个端口,接收 HTTP POST 请求并处理传入的数据
13 0
|
3天前
|
安全 网络协议 网络安全
在Windows7搭建FTP服务器详细教学
在Windows7搭建FTP服务器详细教学
|
3天前
|
Web App开发 安全 Unix
Linux 配置FTP服务器 + vsftpd服务安装配置 (Good篇)
Linux 配置FTP服务器 + vsftpd服务安装配置 (Good篇)
|
3天前
|
数据安全/隐私保护 Windows
使用Serv-U FTP服务器共享文件,实现无公网IP环境下远程访问-2
使用Serv-U FTP服务器共享文件,实现无公网IP环境下远程访问
|
3天前
|
存储 网络协议 文件存储
使用Serv-U FTP服务器共享文件,实现无公网IP环境下远程访问-1
使用Serv-U FTP服务器共享文件,实现无公网IP环境下远程访问
|
3天前
|
弹性计算 关系型数据库 MySQL