java form传值,传递参数以及multipart / form-data上传表单(Java Http Post Upload)

I have a code base which currently uploads file using Post and has enctype as multipart/form-data. Now I need to include some form items i.e. some parameters will also be passed along with the file upload. I have my html form created out but I cannot use request.getParameter because it is a multipart form. Could anyone suggest me how do I pass parameters along with my upload file. I am providing the codes below. Please suggest me how to get around based on compatibility of my codes

if (!ServletFileUpload.isMultipartContent(request)) {

throw new CustomUploadException("Not a file upload request");

}

ServletFileUpload upload = new ServletFileUpload();

FileItemIterator iter = upload.getItemIterator(request);

while (iter.hasNext())

{

FileItemStream item = iter.next();

if (item.isFormField() == false &&

item.getFieldName().equalsIgnoreCase("xmlfile"))

{

String fileName = item.getName();

myBean.setFileName(fileName );

}

}

解决方案

If isFormField on FileItemStream returns true it's a normal field. You can use openStream and read the contents into a String.

Something like this:

FileItemStream item = iter.next();

if(item.isFormField()) {

// Normal field

String name = item.getFieldName();

String value = Streams.asString(item.openStream());

} else {

// File

}

Streams.asString takes a second parameter which is the charset encoding to use, you might need to specify one that is suitable for your site.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值