2017-06-17 68 views
0

当servlet只有一个文件上传时,我知道如何保存它。通过单击只有一个图像icon.But当多个文件servlet如何在一个请求中处理多个上传的文件

Part part = request.getPart("file"); 
File file = new File(filePath); 
try (InputStream inputStream= part.getInputStream()) { // save uploaded file 
    Files.copy(inputStream, file.toPath()); 
} 

例如,在https://stackoverflow.com/questions/ask,用户可以选择上传多张图片:
HTML

<form action="storeArticle" method="post" enctype="multipart/form-data"> 
    <input type="file" name="file"> 
    ... 
</form> 

servlet可以保存上传的文件如下上传一次,servlet如何保存这些上传的文件?
HTML

<input type="file" name="file[]" multiple > 

回答

0
<form action="storeArticle" method="post" enctype="multipart/form-data"> 
<input type="file" name="file"> 
<input type="file" name="file2"> 
<input type="file" name="file3"> 

</form> 

该servlet做到这一点

Part part = request.getPart("file"); 
File file = new File(filePath); 
try (InputStream inputStream= part.getInputStream()) { // save uploaded file 
    Files.copy(inputStream, file.toPath()); 
} 


    Part part = request.getPart("file2"); 
    File file = new File(filePath); 
try (InputStream inputStream= part.getInputStream()) { // save uploaded file 
    Files.copy(inputStream, file.toPath()); 
} 



    Part part = request.getPart("file3"); 
    File file = new File(filePath); 
    try (InputStream inputStream= part.getInputStream()) { // save uploaded file 
    Files.copy(inputStream, file.toPath()); 
    } 
+0

的HTML会显示为3元,这不是我想要的。例如,在https://stackoverflow.com/questions/ask中,用户可以选择仅通过一个图像图标上传多个图像。 – user7328234