zoukankan      html  css  js  c++  java
  • spring 上传 下載文件

    1,spring配置文件添加文件上传配置

    <!-- 上传文件 -->
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <!-- 1GB -->
           <property name="maxUploadSize" value="1073741824" />
           <property name="defaultEncoding" value="utf-8"/>
        </bean>

    2,html

     <form method="post" accept-charset="UTF-8"  th:action="${#httpServletRequest.contextPath+'/upload'}" id="multipartform" enctype="multipart/form-data"  class="form-search" role="form">
                            <input type="file" name="file" size="5" id="file" /><button type="submit" id="multipart" class="btn">上传</button>
                            </form>

    3,controller

     1     @RequestMapping(value = "/upload",method= RequestMethod.POST)
     2     @ResponseBody
     3     public String upload(HttpServletRequest request,HttpServletResponse response,@RequestParam("file") MultipartFile file) {
     4         if (file.isEmpty()) {
     5             return ResponseJSON.instance().setStatus(false).addAlertMessage("请选择导入文件!").toJSON();
     6         }
     7         String filename = file.getOriginalFilename();
     8         int index = filename.indexOf(".");
     9         String file_suffix = filename.substring(index, filename.length());
    10         if (!file_suffix.equals(".txt")) {
    11             return ResponseJSON.instance().setStatus(false).addAlertMessage("请选择txt格式文件!").toJSON();
    12         }
    13         try {
    14             BufferedReader reader = null;
    15             String usernameString = null;
    16             InputStreamReader isr = null;
    17             int line = 1;
    18             try {
    19                 isr = new InputStreamReader(file.getInputStream(), "utf-8");// 考虑到编码格式
    20                 reader = new BufferedReader(isr);
    21                 while ((usernameString = reader.readLine()) != null) {
    22                     system.out.println(usernameString);
    23                     line++;
    24                 }
    25                 
    26             } catch (FileNotFoundException e) {
    27                 // TODO Auto-generated catch block
    28                 logger.error("读取文件失败!", e);
    29             } catch (IOException e) {
    30                 // TODO Auto-generated catch block
    31                 logger.error("读取文件失败!", e);
    32             } finally {
    33                     reader.close();
    34                     isr.close();
    35                     in.close();
    36             }
    37         } catch (IOException e) {
    38             // TODO Auto-generated catch block
    39             logger.error("上传文件失败", e);
    40             return ResponseJSON.instance().setStatus(false).addAlertMessage("上传文件失败").toJSON();
    41         }
    42         return ResponseJSON.instance().setStatus(true).toJSON();
    43     }
    44 
    45     /**
    46      * 写成txt文件格式
    47      * 
    48      * @param path
    49      * @throws IOException
    50      */
    51     @RequestMapping(value = "/load")
    52     public void writeTxt(HttpServletRequest request, HttpServletResponse response) throws IOException {
    53         response.setContentType("text/plain");
    54         response.setCharacterEncoding("UTF-8");
    55         response.setHeader("Content-disposition", "attachment;filename=result.txt");
    56         String name_ip = "这是一个txt文件";
    57         OutputStream ops = null;
    58         try {
    59             ops = response.getOutputStream();
    60             ops.write(name_ip.getBytes());
    61 
    62         } catch (IOException e) {
    63             // TODO Auto-generated catch block
    64             logger.error("写结果文件异常:", e);
    65         } finally {
    66             ops.close();
    67         }
    68 
    69     }
  • 相关阅读:
    37. Sudoku Solver(js)
    36. Valid Sudoku(js)
    35. Search Insert Position(js)
    34. Find First and Last Position of Element in Sorted Array(js)
    33. Search in Rotated Sorted Array(js)
    32. Longest Valid Parentheses(js)
    函数的柯里化
    俞敏洪:我和马云就差了8个字
    vue路由传值params和query的区别
    简述vuex的数据传递流程
  • 原文地址:https://www.cnblogs.com/cora/p/4315889.html
Copyright © 2011-2022 走看看