zoukankan      html  css  js  c++  java
  • java http post上传文件

    1.上传接口

    @IgnoreToken
        @RequestMapping(value = "/upload/cpicFile", method = RequestMethod.POST)
        public void cpicFile(HttpServletResponse response, HttpServletRequest request){
            ErrorCode errorCode = ErrorCode.FAILED;
            Map<String,Object> reqData = this.getContent(request);
            String path = SysConf.CPIC_WORKBASEDIR; //上传路径
            try {
                String name = (String)reqData.get("name");
                String content = (String)reqData.get("content");
                byte[] bytes = org.apache.commons.codec.binary.Base64.decodeBase64(content);
                
                //写入文件
                File fileTmp = new File(path + name); //临时文件
                FileOutputStream fop = new FileOutputStream(fileTmp);
                if (!fileTmp.exists()) {
                    fileTmp.createNewFile();
                }
                fop.write(bytes);
                fop.flush();
                fop.close();
                System.err.println("Done");
                errorCode = ErrorCode.SUCCESS;
            } catch (Exception e){
                logger.error("上传CPIC图片异常",e);
                errorCode = ErrorCode.SYS_ERROR;
            }
            sendResponseContent(response, reqData, errorCode);
        }

    2.测试接口

    @Test
        public void testCpicUploadFile(){
            try {
                Map<String,Object> paramMap=new HashMap<>();
                paramMap.put("name", "cpic-160714.xml");
                File f = new File("C:/sftp/cpic-20160713-3.xml");
                InputStream in = new FileInputStream(f);
                byte[] b = new byte[(int)f.length()];     //创建合适文件大小的数组   
                in.read(b);    //读取文件中的内容到b[]数组   
                in.close();   
                
                String s = new String(b);
                System.out.println("s = " + s);
                System.out.println("b1 = " + b);   //内存地址
                String ss = org.apache.commons.codec.binary.Base64.encodeBase64String(b);      //base64编码
                System.out.println("ss = " + ss);
                System.out.println("b2 = " + Base64.decodeBase64(ss));  //内存地址
                
                paramMap.put("content", ss); 
                //@IgnoreToken
    //            paramMap.put("token", "54ungzbaach65xypcceq48gd6jwlyros");  //token可以加一个注解。
                System.out.println("paramMap="+paramMap);
                HttpResult httpResult = HttpUtil.post("/cpic/upload/cpicFile",paramMap);
                System.out.println(httpResult);
            } catch (Exception e) {
                e.printStackTrace();
            }
            
        }
  • 相关阅读:
    透视校正插值
    投影矩阵推导
    编程思想-小即是美
    Win10使用小技巧
    TotalCommander 之 快捷键
    TotalCommander 之 配置
    TotalCommander 之 日常使用技巧
    上士闻道,勤而行之;中士闻道,若存若亡;下士闻道,大笑之。不笑不足以为道。
    《诫子书》
    青春不是年华,而是心境
  • 原文地址:https://www.cnblogs.com/simpledev/p/5668182.html
Copyright © 2011-2022 走看看