美文网首页
Java 图片上传方式二 : Linux存放图片

Java 图片上传方式二 : Linux存放图片

作者: 王传贺 | 来源:发表于2019-11-21 17:29 被阅读0次

    一 : Java 代码

    @RestController
    @RequestMapping(value = "/v1/upload")
    public class UploadController {
    
        @Autowired
        private IWechatBaseService iWechatService;
    
        /**
         * 图片上传
         * @param files
         * @param request
         * @param response
         * @return
         */
        @RequestMapping(value="/image", method = RequestMethod.POST)
        @ApiOperation("图片上传")
        public JSONData uploadFile(MultipartFile[] files, HttpServletRequest request, HttpServletResponse response){
            List<BaseParameterEntity> list = iWechatService.getListByCode("files_url");
            String[] path = new String[2];
            for (int i=0;i< list.size();i++) {
                path[i]=list.get(i).getValue();
            }
            UploadEntity fileEntity = new UploadEntity();
            try{
                if((files != null && files.length>0)){
                    StringBuffer url = new StringBuffer();
                    for (int i=0;i<files.length;i++) {
                        if(!files[i].isEmpty()) {
                            String pathname = uploadAndGetPathOnly(files[i], request, i, path);
                            if (!StringUtils.isEmpty(pathname)) {
                                url.append(pathname).append(";");
                            }
                        }
                    }
                    //设置页面路径
                    fileEntity.setFiles(url.toString().substring(0, url.length()-1));
    //                String s =url.toString().substring(url.toString().lastIndexOf("/")+1);
    //                fileEntity.setName((s).substring(0, s.length() - 1));
                    //fileEntity.setVisit("http://183.134.200.15:8034/api/images/"+ ((s).substring(0, s.length() - 1)));
                }
            }catch (RRException e){
                e.setMsg("上传文件失败");
                throw e;
            }
            JSONData jsonData = new JSONData();
            jsonData.setData(fileEntity);
            return jsonData;
        }
    
    }
    

    二 :数据库配置

    CREATE TABLE `p_base_parameter` (
      `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
      `name` varchar(100) DEFAULT NULL COMMENT '字段名称',
      `code` varchar(100) DEFAULT NULL COMMENT '字段编号',
      `value` varchar(100) DEFAULT NULL COMMENT '字段值',
      `sort` int(11) DEFAULT '0' COMMENT '排序',
      `dept_id` int(11) DEFAULT NULL COMMENT '部门ID',
      `description` varchar(500) DEFAULT NULL COMMENT '描述',
      `parent_id` int(11) DEFAULT NULL COMMENT '父id',
      `creater` int(11) DEFAULT NULL COMMENT '创建人',
      `create_date` datetime DEFAULT NULL COMMENT '创建时间',
      `last_updater` int(11) DEFAULT NULL COMMENT '更新人',
      `last_update_date` datetime DEFAULT NULL COMMENT '更新时间',
      `status` int(11) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8 COMMENT='基础参数表';
    
    数据配置

    三 : Nginx 配置

    location ~*  \.(apk|gif|jpg|jpeg|png|bmp|doc|docx|ppt|pptx|pdf|xls|xlsx|mp3|mp4)$ {
                root  /usr/local/cci/files/;
                add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Headers X-Requested-With;
            add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
                expires 10d;
                proxy_connect_timeout 90;
                proxy_send_timeout 90;
                proxy_read_timeout 90;
                proxy_buffer_size 4k;
                proxy_buffers 4 32k;
                proxy_busy_buffers_size 64k;
                proxy_temp_file_write_size 64k;
                proxy_ignore_client_abort on;
             }
    

    四 :知识点拓展

    在上述的Nginx 配置,可以发现,拦截所有的 apk|gif|jpg|jpeg|png|bmp|doc|docx|ppt|pptx|pdf|xls|xlsx|mp3|mp4 结尾的资源,导致当其他图片的文件夹路径必须在 /usr/local/cci/files/ ,那怎么独立配置呢? 如下 :

    location ~ /wechat/.*\.(apk|gif|jpg|jpeg|png|bmp|doc|docx|ppt|pptx|pdf|xls|xlsx|mp3|mp4)$ {
                root  /usr/local/cci/files/;
                add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Headers X-Requested-With;
            add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
                expires 10d;
                proxy_connect_timeout 90;
                proxy_send_timeout 90;
                proxy_read_timeout 90;
                proxy_buffer_size 4k;
                proxy_buffers 4 32k;
                proxy_busy_buffers_size 64k;
                proxy_temp_file_write_size 64k;
                proxy_ignore_client_abort on;
             }
    

    这样就可以指定拦截 /wechat 的资源路径了
    例如 : http://ip:80/wechat/images/15746735130920.png
    注意 : 在nginx 上面配置文件路径为 : /usr/local/cci/files/wechat/images

    五 : spring boot 上传文件大小设置
    org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size of 5242880 bytes exceeded; nested exception is org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (5512783) exceeds the configured maximum (5242880)

    servlet:
    multipart:
    max-file-size: 300000000
    max-request-size: 300000000


    image.png

    413 Request Entity Too Large
    nginx http{ }中设置:client_max_body_size 20m;

    没有临时文件
    org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. /tmp/tomcat.5787299030890470771.8034/work/Tomcat/localhost/ROOT/upload_5034069b_14d0_43b8_ab31_55b464831843_00000101.tmp (No such file or directory)

    spring:
    http:
    multipart:
    location: /data/upload_tmp

    相关文章

      网友评论

          本文标题:Java 图片上传方式二 : Linux存放图片

          本文链接:https://www.haomeiwen.com/subject/onptwctx.html