Java apache HttpClient,通过表单上载文件:使用StringBody而不是FileBody
web服务器希望通过html表单上载文件。Java apache HttpClient,通过表单上载文件:使用StringBody而不是FileBody,java,http,httpclient,Java,Http,Httpclient,web服务器希望通过html表单上载文件。 这就是我构建多党体的方式,它已经起作用了: FileBody filePart = new FileBody(new File("emptyFile.txt"), "text/plain"); FormBodyPart fbp = new FormBodyPart("UploadService", filePart); MultipartEntity mpe = new MultipartEntity(); mpe.addPart(fbp); 事
这就是我构建多党体的方式,它已经起作用了:
FileBody filePart = new FileBody(new File("emptyFile.txt"), "text/plain");
FormBodyPart fbp = new FormBodyPart("UploadService", filePart);
MultipartEntity mpe = new MultipartEntity();
mpe.addPart(fbp);
事实上,我的数据在内存中,所以我不喜欢将其保存到磁盘的想法,所以我尝试替换它
FileBody filePart = new FileBody(new File("emptyFile.txt"), "text/plain");
与
但是第二种方法不起作用,服务器返回一个HTTP500异常;将数据记录在电线上,我注意到唯一的区别如下:
使用FileBody时的HTTP POST跟踪:
...
Content-Disposition: form-data; name="UploadService"; filename="emptyFile.txt"
...
...
Content-Disposition: form-data; name="UploadService"
...
使用StringBody时的HTTP POST跟踪:
...
Content-Disposition: form-data; name="UploadService"; filename="emptyFile.txt"
...
...
Content-Disposition: form-data; name="UploadService"
...
也就是说,在FileBody上载中,指定了一个在StringBody上载中未指定的“文件名”。
如何修复此问题?使用
StringBody
您将无法修复。但是您可以扩展StringBody
并覆盖getFilename()
以不返回null
根据的来源,这应该足以拥有所需的filename parm参数:
protected void generateContentDisp(final ContentBody body) {
StringBuilder buffer = new StringBuilder();
buffer.append("form-data; name=\"");
buffer.append(getName());
buffer.append("\"");
if (body.getFilename() != null) {
buffer.append("; filename=\"");
buffer.append(body.getFilename());
buffer.append("\"");
}
addField(MIME.CONTENT_DISPOSITION, buffer.toString());
}
使用
StringBody
时,您将无法使用。但是您可以扩展StringBody
并覆盖getFilename()
以不返回null
根据的来源,这应该足以拥有所需的filename parm参数:
protected void generateContentDisp(final ContentBody body) {
StringBuilder buffer = new StringBuilder();
buffer.append("form-data; name=\"");
buffer.append(getName());
buffer.append("\"");
if (body.getFilename() != null) {
buffer.append("; filename=\"");
buffer.append(body.getFilename());
buffer.append("\"");
}
addField(MIME.CONTENT_DISPOSITION, buffer.toString());
}
只是把这个放在这里,因为我有问题,一直在我的搜索中来这里。。。类似于上面的方法,但我想它更简单
public class StringFileBody extends FileBody {
private String data;
public StringFileBody( String input, ContentType contentType ) {
super( new File( "." ), contentType );
data = input;
}
public StringFileBody( String input, ContentType contentType, String fileName ) {
super( new File( "." ), contentType, fileName );
data = input;
}
@Override
public void writeTo( OutputStream out ) throws IOException {
out.write( data.getBytes( ) );
}
@Override
public long getContentLength( ) {
return data.getBytes( ).length;
}
}
只是把这个放在这里,因为我有问题,一直在我的搜索中来这里。。。类似于上面的方法,但我想它更简单
public class StringFileBody extends FileBody {
private String data;
public StringFileBody( String input, ContentType contentType ) {
super( new File( "." ), contentType );
data = input;
}
public StringFileBody( String input, ContentType contentType, String fileName ) {
super( new File( "." ), contentType, fileName );
data = input;
}
@Override
public void writeTo( OutputStream out ) throws IOException {
out.write( data.getBytes( ) );
}
@Override
public long getContentLength( ) {
return data.getBytes( ).length;
}
}
什么是服务器框架,服务器端的错误日志是什么?对我来说,这应该在服务器上得到修复,因为filename parm参数是完全可选的,只应被视为提示,所以如果提供了无效的参数(或者没有人,如本例中所示)服务器就不会崩溃。这很有趣,但在这里我想改进对HttpClient API的控制。什么是服务器框架,服务器端的错误日志上到底写了什么?对我来说,这应该在服务器上得到修复,因为filename parm参数是完全可选的,只应被视为提示,所以如果提供了无效的参数(或者没有人,就像在本例中)服务器就不会崩溃。这很有趣,但在这里我想改进对HttpClient API的控制。