0

I am trying upload image to server via HttpUrlConnection but it's not working.

return internal server 500

anyone can help me? thats postman captures 1: http://i62.tinypic.com/6e279j.png 2: http://oi62.tinypic.com/6e279j.jpg

and that is my code block

 url = new URL(urls[0]);
                // POST header
            File image = new File(outFile);
                HttpURLConnection con = (HttpURLConnection)
                        url.openConnection();
                con.setDoInput(true);
                con.setDoOutput(true);
                con.setRequestMethod("POST");
                con.setRequestProperty("Connection", "keep-alive");
                con.setRequestProperty("ApiKey", ApiKey);
                con.setRequestProperty("AuthKey", AuthKey);
                con.setRequestProperty("Content-Disposition","form-data; name=\"fileUpload\"filename="+image.getName());
                con.setRequestProperty("Content-Type","image/jpg");
                con.setRequestProperty("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary8FAIDy4eUDwgPYCA");

                // POST body (copied from file stream)
                int i;
                byte[] buff = new byte[8192];
                FileInputStream fis = new FileInputStream(image);
                OutputStream os = con.getOutputStream();
                while((i = fis.read(buff)) == 8192)
                    os.write(buff);
                os.write(buff, 0, i);
                os.flush();
                os.close();
                con.connect();
                Log.w("response",con.getResponseMessage()+" - " +con.getResponseCode() +" - "+ image.getName());
                // Read response (still needed)
                String text = null;

finally server side var _uploadDirectory = Path.Combine(pathProvider.GetRootPath(), "upImages"); var _filename = Helper.GetGUID();//Path.ChangeExtension(Helper.GetGUID(), "png");

            if (Request.Files.Count() != 1)
                return new NaberResponse("error", "invalid parameters");

            try
            {
                if (!Directory.Exists(_uploadDirectory))
                    Directory.CreateDirectory(_uploadDirectory);

                var _file = Request.Files.First();

                using (var _fileStream = new FileStream(Path.Combine(_uploadDirectory, Path.ChangeExtension(_filename, "jpg")), FileMode.Create))
                {
                    _file.Value.CopyTo(_fileStream);
                }

                return new NaberResponse(pData: new { media_url = string.Format("{0}/post/image/{1}", Request.Url.SiteBase, _filename) });
  • Please follow this link :http://stackoverflow.com/questions/11766878/sending-files-using-post-with-httpurlconnection – Rajan Bhavsar Aug 19 '15 at 13:21
  • You can try [my answer at How to add parameters to HttpURLConnection using POST](http://stackoverflow.com/questions/9767952/how-to-add-parameters-to-httpurlconnection-using-post/31888919#31888919) – BNK Aug 28 '15 at 03:10
  • thank you but i solved my problem via another one code. – Hayrettin Çetintaş Aug 28 '15 at 10:49
  • @hiddenhazard can you please share the code that you used to solve the issue , cause i am also facing the same issue. – Gaurav Sarma Oct 17 '16 at 10:51

0 Answers0