• ios 文件上传到SpringMVC


    ios代码:
    -(void)sendImg:img:(UIImage *)image{

     //请求地址
        NSMutableString *url = [[NSMutableString alloc] init];
        [url appendString:[UtilTool getHostURL]];
        [url appendString:@"savePic"];
        
        NSString *TWITTERFON_FORM_BOUNDARY = @"AaB03x";
        //分界线 --AaB03x
        NSString *MPboundary=[[NSString alloc]initWithFormat:@"--%@",TWITTERFON_FORM_BOUNDARY];
        //结束符 AaB03x--
        NSString *endMPboundary=[[NSString alloc]initWithFormat:@"%@--",MPboundary];
        
        NSMutableString *body = [[NSMutableString alloc] init];   


        [body appendFormat:@"%@ ",MPboundary];

      //请求参数
        [body appendFormat:@"Content-Disposition: form-data;name="%@" ",@"token"];

      //参数值
        [body appendFormat:@"%@ ", [UtilTool getToken]];
        
        NSData *imageData = UIImagePNGRepresentation([UtilTool changeImg:image max:1136]);
        
        //声明myRequestData,用来放入http body
        NSMutableData *myRequestData;
        //将body字符串转化为UTF8格式的二进制
        myRequestData=[NSMutableData data];
        

      //上传文件
        [body appendFormat:@"%@ ",MPboundary];
        [body appendFormat:@"Content-Disposition: form-data; name="uploadFile"; filename="%@" ",@"temp.png"];
        [body appendFormat:@"Content-Type: image/png "];
        [myRequestData appendData:[body dataUsingEncoding:NSUTF8StringEncoding]];
        
        [myRequestData appendData:imageData];
        
        //声明结束符:--AaB03x--
        NSString *end=[[NSString alloc]initWithFormat:@" %@",endMPboundary];
        //加入结束符--AaB03x--
        [myRequestData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
        
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:url]];
        [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
        //    [request setTimeoutInterval:[DataStore getHttpTimeout]];
        [request setHTTPMethod:@"POST"];
        //设置HTTPHeader中Content-Type的值
        NSString *cttype=[[NSString alloc]initWithFormat:@"multipart/form-data; boundary=%@",TWITTERFON_FORM_BOUNDARY];
        //设置HTTPHeader
        [request setValue:cttype forHTTPHeaderField:@"Content-Type"];
        //设置Content-Length
        [request setValue:[NSString stringWithFormat:@"%ld", [myRequestData length]] forHTTPHeaderField:@"Content-Length"];
        [request setHTTPBody:myRequestData];
        [NSURLConnection sendSynchronousRequest:request returningResponse:nil error: nil];
    }

    后台代码:
    @RequestMapping(value = {"/savePic"}, method = RequestMethod.POST, produces= { "application/json" })
        @ResponseBody
        public String savePic(MultipartFile uploadFile, HttpServletRequest request) throws IOException, JSONException{
            JSONObject object=new JSONObject();
            if(uploadFile != null){            //上传文件
                String path = request.getSession().getServletContext().getRealPath("filePath");
                String fileName = uploadFile.getOriginalFilename();
                String fileType = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
                
                File toFile = null;
                File targetFile = new File(fileName);
                //修改文件名字,格式取当前时间
                toFile = new File(path, DateVaildator.simpleDateFormat(new Date(), "yyyyMMddHHmmss") + fileType);
                targetFile.renameTo(toFile);
                if (!toFile.exists()) {
                    toFile.mkdirs();
                }
                //保存至我们服务器
                uploadFile.transferTo(toFile);
                try {
                    String downloadUrl="filePath/"+toFile.getName();
                    object.put(Constants.RESULT_STATUS, Constants.RESULT_STATUS_ERROR);
                    object.put(Constants.RESULT_MSG, "保存失败");
                } catch (Exception e) {
                    e.printStackTrace();
                    object.put(Constants.RESULT_STATUS, Constants.RESULT_STATUS_SUCCESS);
                    object.put(Constants.RESULT_MSG, downloadUrl);
                }
                toFile.delete();
            }
            return object.toString();
        }

  • 相关阅读:
    Python面试进阶问题,__init__和__new__的区别是什么?
    LeetCode 76,一题教会你面试算法时的思考套路
    LeetCode 75,90%的人想不出最佳解的简单题
    手把手带你入门numpy,从此数据处理不再慌【四】
    一次找出范围内的所有素数,埃式筛法是什么神仙算法?
    机器学习——十大数据挖掘之一的决策树CART算法
    TypeScript环境搭建
    ASP.NET 开源导入导出库Magicodes.IE 完成Excel图片导入导出
    【翻译】.NET 5 Preview 1 发布
    SuperBenchmarker一个用.NET编写的压测工具
  • 原文地址:https://www.cnblogs.com/shareze/p/4064233.html
Copyright © 2020-2023  润新知