2017-06-17 97 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