PrintWriter out = response.getWriter();
System.out.println("Request content length is " + request.getContentLength() + "<br/>");
System.out.println("Request content type is " + request.getHeader("Content-Type") + "<br/>");
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(isMultipart){
ServletFileUpload upload = new ServletFileUpload();
try{
FileItemIterator iter = upload.getItemIterator(request);
FileItemStream item = null;
String name = "";
InputStream stream = null;
while (iter.hasNext()){
item = iter.next();
name = item.getFieldName();
stream = item.openStream();
if(item.isFormField()){System.out.println("Form field " + name + ": "
+ Streams.asString(stream) + "<br/>");}
else {
name = item.getName();
System.out.println("name==" + name);
if(name != null && !"".equals(name)){
String fileName = new File(item.getName()).getName();
out.write("Client file: " + item.getName() + " <br/>with file name "
+ fileName + " was uploaded.<br/>");
File file = new File(getServletContext().getRealPath("/WEB-INF/temp/" + fileName));
FileOutputStream fos = new FileOutputStream(file);
long fileSize = Streams.copy(stream, fos, true);
System.out.println("Size was " + fileSize + " bytes <br/>");
System.out.println("File Path is " + file.getPath() + "<br/>");
}
}
}
} catch(FileUploadException fue) {out.write("fue!!!!!!!!!");}
}
How to save more than one paramerters passed to a servlet from a multipart form?
-
05-10-2022 - |
Frage
I couldn't figure out how to use getParts()
to save the parameters.
I am trying to pass 4-5 inputs of type text along with an image file. I want to retrieve the parameters into strings so I can add them to the database and save the image in a blob format in the database (not in the server directory).
Any Help would be Highly appreciated. Thanks in Advance
<form class="form-signin" action="createAlbum"
method="post" enctype="multipart/form-data" role="form">
<h2 class="form-signin-heading">
Add details to create an Album.
</h2>
<input type="text" name="aname"
class="form-control" placeholder="Album Name" required>
<input type="text" name="artists"
class="form-control" placeholder="Artist" required>
<input type="date" name="rdate"
class="form-control" placeholder="Release Date" required >
<input type="text" name="type"
class="form-control" placeholder="Genre" required >
<input type="text" name="price"
class="form-control" placeholder="Price">
<input type="file" name="picture"
class="form-control" placeholder="Picture" required >
<button class="btn btn-lg btn-primary btn-block"
type="submit">Add Album</button>
</form>
Lösung 2
Andere Tipps
You may give apache FileUpload a try
http://commons.apache.org/proper/commons-fileupload/using.html
then you could process your multipart request like this
// Process the uploaded items
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
FileItem item = iter.next();
if (item.isFormField()) {
processFormField(item);
} else {
processUploadedFile(item);
}
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow