我使html頁面上傳圖像與文字框有它的描述。 我已經使用multipart/form-data;在servlet的dopost我得到的文件使用 ServletFileUpload upload = new ServletFileUpload();如何使用multipart/form-data從請求中獲取字符串參數?
獲取字符串參數我使用request.getparameter(); 但它總是給我NULL? 你能告訴我嗎?
HTML ::
<form name="filesForm" action="/upload" method="post" enctype="multipart/form-data">
File : <input type="file" name="file">
<textarea name = "description"
rows = "4" cols = "30">Enter comments here.</textarea>
<input type="submit" name="Submit" value="Upload File">
在servlet的:
ServletFileUpload upload = new ServletFileUpload();
upload.setSizeMax(500000000);
FileItemIterator iterator = null;
try {
iterator = upload.getItemIterator(req);
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // to handle contents or
// instances of request.
FileItemStream item = null;
try {
item = iterator.next();
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // get access each item on iterator.
java.io.InputStream in = item.openStream(); // allows to read the items contents.
Blob imageBlob = new Blob(IOUtils.toByteArray(in)); // store each item on Blob
// object after converting them to Bytes.
/*……….
Note: we are concerned on uploading images files. So the type of files is either (.jpg or gif)
……….
*/
PersistenceManager pm = PMF.get().getPersistenceManager();
String counter="1";
ServletOutputStream outt = resp.getOutputStream();
//match incoming request Content type and invoke appropriate method for handel request
怎麼樣? ??你可以給我一個例子嗎 ??? – Hanaa
你檢查了鏈接嗎? –
我嘗試過,但現在它給我例外 – Hanaa