0
votes

I use this method to post data to my java servlet, but getParameter("2") is null, but I can still get the image.

 let dics = NSMutableDictionary()
    dics.setObject(1, forKey: "2")
    dics.setObject(2, forKey: "3")
    manager.responseSerializer.acceptableContentTypes = NSSet(object: "text/plain") as Set<NSObject>

    manager.POST(urlstr, parameters: dics, constructingBodyWithBlock: {
        fromData in
        fromData.appendPartWithFormData(imgdata, name: "userImg")
    }, success: { (operation, responseObject)in
        println("bingo")
    }) { (operation, error) -> Void in
        println(error)
    }

My question is:

  1. How can I get the parameters?

  2. and I want to know appendPartWithFormData(imgdata, name: "userImg"),how should I use the "userImg" ?

I use request.getInputStream() to get the image..

3
Which parameters and where? You posted a code for AFNetworking, but it seems like you're asking for how to get it in Java on the server side? - Michal

3 Answers

0
votes

Try Alamofire framework, i am sure to solve your problem.

You can upload multipart form data like this;

Alamofire.upload(
.POST,
URLString: "http://httpbin.org/post",
multipartFormData: { multipartFormData in
    multipartFormData.appendBodyPart(fileURL: unicornImageURL, name: "userImg")

},
encodingCompletion: { encodingResult in
    switch encodingResult {
    case .Success(let upload, _, _):
        upload.responseJSON { request, response, JSON, error in
            println(JSON)
        }
    case .Failure(let encodingError):
        println(encodingError)
    }
}

)

0
votes

I have solved this problem! :^)

The way I post to server is correct,but the way I get the parameter is wrong. Not a problem about the iOS or AFN.

Because I post multi form data to server,so I cant just use request.getParameter("2") to get the parameter.

And the parameter "name" in appendPartWithFormData(imgdata, name: "userImg")is to separate the data from other data.

For how to get the multipart/form-data parameters in a Servlet,please click here:get multipart/form-data parameters in servlet

-1
votes

Upload image using AFNetworking in Swift 4

func uploadUsingPOST_Image (urlPath: NSString, withParameter dictData: NSMutableDictionary, withImage image: UIImage, WithImageName imageName: NSString,successBlock success:@escaping (_ responceData:AnyObject)->Void) {

    let manager = AFHTTPRequestOperationManager()

    var Timestamp: String {
        return "\(NSDate().timeIntervalSince1970 * 1000)"
    }

    let operation =  manager.post(urlPath as String, parameters: dictData, constructingBodyWith: { (data:AFMultipartFormData!) -> Void in

        if image != nil {

            data.appendPart(withFileData: UIImagePNGRepresentation(image!)!, name: imageName as String, fileName: "\(Timestamp).png", mimeType: "image/png")


        }
    }, success: { (operation, responseObject) -> Void in

        success(responseObject as AnyObject)
    }) { (operation, error) -> Void in

        print(error, terminator: "")
    }

    operation?.start()
}