0

I wrote some code, want to test a upload-Image feature(try to upload an image in my iPhone to the Mac)

I use Alamofire4 to upload the image in Xcode8, then create a javaWeb to receive that image by Serlvet.

I think I receive the DATA of the image in Mac after the test, but I CAN NOT open the image. I know I haven't parse the DATA(MultipartFormData created by Alamofire4).

SO, it's my question: how to parse this DATA in Serlvet to show the real image not the imageData in my Mac I think it is too easy for javaWeb developer, but it is hard for me Show me the modification please.

This is the swift code using Alamofire4:

@IBAction func chooseImage(_ sender: UIButton) {
    var picker : UIImagePickerController
    picker = UIImagePickerController()
    picker.delegate = self
    picker.sourceType = UIImagePickerControllerSourceType.photoLibrary
    self.present(picker, animated: true, completion: nil)

}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    myImage.image = info[UIImagePickerControllerOriginalImage] as! UIImage?
    self.dismiss(animated: true, completion: nil)

}

@IBAction func uploadData(_ sender: UIButton) {
    let imageData = UIImageJPEGRepresentation(myImage.image!, 1)

    imageRename = myText.text!+".jpeg"

    Alamofire.upload(multipartFormData:{(multiData) in
        multiData.append(imageData!, withName: "imageMine", fileName: self.imageRename!, mimeType: "image/*")},
                     usingThreshold:UInt64.init(),
                     to:"http://192.168.0.101:8080/John_SImage/imageServlet",
                     method:.post,
                     headers:nil,
                     encodingCompletion: { encodingResult in
                        switch encodingResult {
                        case .success(let upload, _, _):
                            upload.responseJSON { response in
                                debugPrint(response)
                            }
                        case .failure(let encodingError):
                            print(encodingError)
                        }
    })
}

and here is the code in Servlet (javaWeb) below:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println("1111");
    request.setCharacterEncoding("UTF-8");
    response.setContentType("text/plain;charset=utf-8");
    PrintWriter writer=response.getWriter();    
    InputStream in=request.getInputStream();
    File f = new File("/Users/BC/Desktop/ff.jpeg");
    FileOutputStream fout = new FileOutputStream(f);
    byte[] b=new byte[1024];
    int n=0;
    while ((n=in.read(b))!=-1){
        fout.write(b,0,n);
    }
    fout.close();
    in.close();
    writer.println("Finished uploading files!");
    writer.close();

}
Braver Chiang
  • 351
  • 2
  • 13
  • In the duplicate just skip the client side (HTML) part. Server side code is exactly the same regardless of the client technology used. – BalusC Nov 12 '16 at 11:10
  • I read THAT answers carefully, It works! Your recommendation is so great. I've spend more than 3 hours to find a solution before I ask this question, then I just fix this issue in less than 10 minutes after read the RIGHT answer you recommend. – Braver Chiang Nov 12 '16 at 12:28

0 Answers0