从像curl一样从java(图像和一些参数)发送表单数据
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了从像curl一样从java(图像和一些参数)发送表单数据,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3202字,纯文字阅读大概需要5分钟。
内容图文
![从像curl一样从java(图像和一些参数)发送表单数据](/upload/InfoBanner/zyjiaocheng/722/367cdef2935040f4b542adc9e4b631fe.jpg)
我有这个运行此命令,它给了我正确的输出,我想在Java中使用HttpUrlConnection(或任何第三方库)模拟这个
curl http://localhost/myservice/ -v -H 'Content-Type: multipart/form-data' -F 'file=@/home/xyz/abc.jpg' -F 'type=image' -F 'id=123'
该服务接受文件,类型和ID.
我目前的代码看起来像这样 –
String urlToConnect = "http://localhost/myservice/";
String boundary = Long.toHexString( System.currentTimeMillis() ); // Just generate some unique random value
HttpURLConnection connection = (HttpURLConnection) new URL( urlToConnect ).openConnection();
connection.setDoOutput( true ); // This sets request method to POST.
connection.setRequestProperty( "Content-Type", "multipart/form-data; boundary="+boundary);
PrintWriter writer = null;
try
{
writer = new PrintWriter( new OutputStreamWriter( connection.getOutputStream(), "UTF-8" ) );
writer.println( "--" + boundary );
// -F type =1
writer.println( "Content-Disposition: form-data; name=\"type\"" );
writer.println( "Content-Type: text/plain; charset=UTF-8" );
writer.println();
writer.println( "image" );
//writer.println( "--" + boundary );
// -F id=1
writer.println( "Content-Disposition: form-data; name=\"id\"" );
writer.println( "Content-Type: text/plain; charset=UTF-8" );
writer.println();
writer.println( 123 );
//writer.println( "--" + boundary );
writer.println( "Content-Disposition: form-data; name=\"file\"; filename=\"abc.jpg\"" );
writer.println( "Content-Type: image/jpeg;" );
writer.println();
BufferedReader reader = null;
try
{
reader = new BufferedReader( new InputStreamReader( new FileInputStream(
"/home/xyz/abc.jpg" ), "UTF-8" ) );
for( String line; (line = reader.readLine()) != null; )
{
writer.println( line );
}
}
finally
{
if( reader != null ) try
{
reader.close();
}
catch( IOException logOrIgnore )
{
}
}
}
finally
{
if( writer != null ) writer.close();
}
// Connection is lazily executed whenever you request any status.
int responseCode = ((HttpURLConnection) connection).getResponseCode();
System.out.println( responseCode ); // Should be 200
StringBuffer responseContent = new StringBuffer();
BufferedReader rd = null;
try
{
rd = new BufferedReader( new InputStreamReader( connection.getInputStream() ) );
}
catch( Exception e )
{
rd = new BufferedReader( new InputStreamReader( connection.getErrorStream() ) );
}
String temp = null;
while( (temp = rd.readLine()) != null )
{
responseContent.append( temp );
}
System.out.println( "Response : " + responseContent );
}
解决方法:
我已经尝试使用apache httpClient,现在我的代码看起来像
HttpClient httpclient = new HttpClient();
File file = new File( "/home/abc/xyz/solar.jpg" );
// DEBUG
logger.debug( "FILE::" + file.exists() ); // IT IS NOT NULL
try
{
PostMethod filePost = new PostMethod( "http://localhost/myservice/upload" );
Part[] parts = { new StringPart( "type","image"),new StringPart( "id","1"), new FilePart( "file", file ) };
filePost.setRequestEntity( new MultipartRequestEntity( parts, filePost.getParams() ) );
// DEBUG
int response = httpclient.executeMethod( filePost );
logger.info( "Response : "+response );
logger.info( filePost.getResponseBodyAsString());
}
catch( HttpException e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch( IOException e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
现在我得到正确的响应代码.
内容总结
以上是互联网集市为您收集整理的从像curl一样从java(图像和一些参数)发送表单数据全部内容,希望文章能够帮你解决从像curl一样从java(图像和一些参数)发送表单数据所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。