2015-05-28 7 views
0

я получил перетащить поле ввода файла:getParts() получить текстовые входы слишком

<div style="margin-bottom: 1em;"> 
    <div class="file-well">     
     <input type="file" name="file" id="file" multiple/> 
     <span id="filewelllabel">Aby dodać CV kliknij tutaj lub przeciągnij i upuść plik w ten obszar.<br/></span> 
     <span id="filewelllabel">Możesz dodać jeden plik (.pdf, .doc, .docx, .docm)</span> 
    </div> 
</div> 

И это моя загрузка сервлет ручка:

Collection<Part> fileParts = request.getParts(); 
String uploadPath= "\\upload\\"; 

String savedFileName; 

for (Part part : fileParts) { 
    fileName = getFileName(part); 
    savedFileName= temp.getImie() + temp.getNazwisko() + fileName; 
    part.write(uploadPath+ File.separator + savedFileName); 
} 

При добавлении файлов в формате PDF, это лог консоли :

Info: content-disposition header= form-data; name="imie" 
Info: content-disposition header= form-data; name="nazwisko" 
Info: content-disposition header= form-data; name="telefon" 
Info: content-disposition header= form-data; name="email" 
Info: content-disposition header= form-data; name="uczelnia" 
Info: content-disposition header= form-data; name="doswiadczenie" 
Info: content-disposition header= form-data; name="zainteresowania" 
Info: content-disposition header= form-data; name="czySlyszales" 
Info: content-disposition header= form-data; name="zrodlo" 
Info: content-disposition header= form-data; name="file"; filename="cv — kopia.pdf" 
Info: content-disposition header= form-data; name="file"; filename="cv.pdf" 
Info: content-disposition header= form-data; name="zgoda" 

и это результат в папке загрузки enter image description here

Как я могу предотвратить сохранение других файлов, кроме pdf? Должны ли они принимать только части ввода файлов?

ответ

0
if (fileName != null && !fileName.isEmpty()){ 

или

if(part.getContentType() != null){ 

в файле сервлета было решение.

2

Иначе вы можете проверить это следующим образом:

if (part.getName().equals("file")) { 

Теперь только входы с именем = «файл» проходит проверку. Он вернет вам тот же результат, но я думаю, что это лучше.

Смежные вопросы