I am able to tweet using API but when I want to Upload an Image I am getting Internal Error(error code:500)

media-upload
java

#1
public void buildMultiPartFormFieldData()
{
	
	try
	{
		this.outputStream = connection.getOutputStream();
		this.data = new DataOutputStream(outputStream);

		for(Map.Entry<String,Object> formdata : formData.entrySet())
		{
			
			String key =formdata.getKey();
			writeToStream(data,boundary);
			writeToStream(data,CRLF);
			String className = formdata.getValue().getClass().getName();
			if((key.equals("media") && key.equals("file")) || (className.indexOf("Stream")>-1) || (className.indexOf("File")>-1 ))
			{
				writeToStream(data,"Content-Disposition: form-data; name=\""+key+"\"; filename=\"sheik.jpg\"");
				writeToStream(data,CRLF);
				writeToStream(data,"Content-Type:form-data;image/jpeg");
				writeToStream(data,CRLF+CRLF);
		
				BufferedInputStream in = new BufferedInputStream((FileInputStream) formdata.getValue());
			
				byte[] buff = new byte[1024];
				int length;
				while ((length = in.read(buff)) != -1)
				{
					data.write(buff, 0, length);
				}
				writeToStream(data,CRLF);
				in.close();
			}
			else
			{
				writeToStream(data,"Content-Disposition: form-data; name=\""+key+"\"");
				writeToStream(data,CRLF);
				writeToStream(data,"Content-Type:text/plain;charset=UTF-8");
				writeToStream(data,CRLF+CRLF);
				data.write(((String)formdata.getValue()).getBytes("UTF-8"));
				writeToStream(data,CRLF);
			}
		}
		writeToStream(data,boundary + "--"+CRLF);
		writeToStream(data,CRLF);
		data.flush();
                    data.close();
	}
	catch(IOException ioe)
	{
		ioe.printStackTrace();
		logger.info("I/O Error");
	}
	
}

public void writeToStream(DataOutputStream dataStream,String value)
{
	try
	{
		dataStream.writeBytes(value);
		System.out.println(value);
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}

}

#2

Here’s a working sample request to upload media to Twitter, maybe you can adapt your Java code to send something similar:

POST /1.1/media/upload.json HTTP/1.1
Host: upload.twitter.com
User-Agent: codebird-php/3.0.0 +https://github.com/jublonet/codebird-php
Accept: */*
Content-Type: multipart/form-data; boundary=--------------------cd08df68
Authorization: OAuth oauth_consumer_key="(cut for forum)", oauth_nonce="4258c84d", oauth_signature="dp0594RNxFzH6uSvc%2Fdx0ZjEfw8%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1451675642", oauth_token="1712755268-(cut for forum)", oauth_version="1.0"
Content-Length: 135117

----------------------cd08df68
Content-Disposition: form-data; name="media"

����JFIF.....(shortened for forum).....��T���
----------------------cd08df68--

#3

@john_stance Have you succeeded?