0
我知道也許它可能是一個重複的問題,我在網上閱讀類似的帖子,但我不能發送我的輸入值從JSP到servlet。 我可以上傳文件,但我需要發送一個輸入值。 的Servlet:如何將輸入值從jsp中的multipart/form-data傳遞給servlet?
try {
FileItemIterator iterator = upload.getItemIterator(req);
while (iterator.hasNext()) {
FileItemStream item = iterator.next();
InputStream in = item.openStream();
if (item.isFormField()) {
out.println("Got a form field: " + item.getFieldName());
} else {
String fieldName = item.getFieldName();
String fileName = item.getName();
String contentType = item.getContentType();
String fileNameWithOutExt = FilenameUtils.removeExtension(fileName);
out.println("--------------");
out.println("fileName = " + fileName);
out.println("Kind = " + fileNameWithOutExt);
out.println("field name = " + fieldName);
out.println("contentType = " + contentType);
String fileContents = null;
try {
fileContents = IOUtils.toString(in);
out.println("lenght: " + fileContents.length());
String[] content = fileContents.split("\n");
String[] property = content[0].split(",");
out.println("lunghezza " + property.length);
for (String propr : property) {
out.println("prop: " + propr);
}
//out.println(Arrays.toString(property));
//out.println(fileContents);
} finally {
IOUtils.closeQuietly(in);
}
}
}
} catch (SizeLimitExceededException e) {
out.println("You exceeded the maximu size ("
+ e.getPermittedSize() + ") of the file ("
+ e.getActualSize() + ")");
}
} catch (Exception ex) {
throw new ServletException(ex);
}
}
這就是我的JSP
<form action="/upload_kind" method="post" enctype="multipart/form-data" >
<input type="file" name="file" size="50" />
<input type="text" name="namespace" id="namespace">
<br />
<input type="submit" value="Upload File" />
</form>
我讀到了 「item.getString()」 方法,但它在DiskFileItem類,而不是在FileItemStream,這樣我就可以看到「命名空間「發送,但我看不到它的價值。 Thx和對不起我的英語不好
你的輸入值是多少?如果這是命名空間,您應該可以通過request.getParameter(「namespace」)獲取它。 –
它是命名空間,但我無法得到它的原因「multipart/form-data」。它返回null – ValeMarz
[如何使用JSP/Servlet將文件上傳到服務器?](http://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet ) –