• springmvc利用jqueryupload上传文件,后台处理方法


        public void importIdentifySchemeFile(Integer id,Integer type,HttpServletRequest request, HttpServletResponse response){
         

       PrintWriter out = null;
              try {
                  //初始化变量
                  InputStream stream = null;
                  String ret_fileName = null;//返回给前端已修改的文件名称
                  Map<String, Object> result = new HashMap<String, Object>();
               SkillIdentifyFile skillIdentifyFile = new SkillIdentifyFile();
                  String fileSrc;
                  String fileName;
                  File uploadedFile = null;
                  try {
                   
                      //设置字符集
                      request.setCharacterEncoding("UTF-8");
                      response.setContentType("text/html; charset=UTF-8");
                      out = response.getWriter();
                      if(id==null){
                 result.put("message", "方案不存在,不能上传");
                          out.print(new JSONObject(result).toString());
                          return;
                }
                      //获取文件流
                      MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
                      Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
                      MultipartFile multipartFile = null;
                      for (Map.Entry<String, MultipartFile> set : fileMap.entrySet()) {
                          multipartFile = set.getValue();//文件名
                      }
                      stream = multipartFile.getInputStream();

                      //初始化文件名,并定义上传文件
                      UUID uuid = UUID.randomUUID();
                       fileName = multipartFile.getOriginalFilename();//原始文件名
                      String suffix = fileName.substring(fileName.indexOf(".") + 1);
                     
                      ret_fileName = uuid.toString() + "." + suffix;
                   // 文件保存目录路径
                      String savePath = FileSystemConfig.getFileRootPath() + FILE_PATH;                
                      // 创建文件夹
                      File dirFile = new File(savePath);
                    
                      if (!dirFile.exists()) {
                          dirFile.mkdirs();
                      }
                      fileSrc = savePath + File.separator + ret_fileName;
                      uploadedFile = new File(fileSrc);
                      FileUtils.saveFile(uploadedFile, stream);
                  } catch (Exception e) {
                      //读写错误异常处理
                      result.put("message", e.getMessage());
                      out.print(new JSONObject(result).toString());
                      return;
                  } finally {
                      //资源关闭异常处理
                      try {
                          if (stream != null) {
                              stream.close();
                          }
                      } catch (Exception e) {
                          result.put("message", e.getMessage());
                          out.print(new JSONObject(result).toString());
                          return;
                      }
                  }
                    
                //初始化方案附件属性
                  skillIdentifyFile.setFile(FILE_PATH + ret_fileName);//下载路径
                  skillIdentifyFile.setName(fileName);//原始文件名
                  skillIdentifyFile.setUpdateTime(new Date(System.currentTimeMillis()));
                  skillIdentifyFile.setSkillidentifyId(id);
                  skillIdentifyFile.setType(type);
                    
                  //保存当前附件到数据库
                  skillIdentifyService.addSkillIdentifyFile(skillIdentifyFile);
                 
                      result.put("message", File_SUCCESS);
                      result.put("fileName", fileName);
                      out.print(new JSONObject(result).toString());
                 
              } finally {
                  //将信息返回
                  if (out != null) {
                      out.flush();
                      out.close();
                  }
              }
         }

  • 相关阅读:
    v-distpicker 一个好用的三级联动的插件
    截取url参数
    position: relative 和 position: absoution 的详解
    自己封装一个下拉列表
    临界区访问的两个类:临界区类,共享临界区类——多平台版本
    临界区访问的两个类:临界区类,共享临界区类
    beyond compare 4.2.9桌面右键集成的问题修复
    递归创建、删除目录的几个函数
    读取资源中的GIF文件相应像素宽高度
    在关于对话框中,自动获取当前程序的版本,以显示
  • 原文地址:https://www.cnblogs.com/gtaxmjld/p/4330168.html
Copyright © 2020-2023  润新知