2012-12-19 16 views
8

का उपयोग करते समय फ़ाइल नाम पुनर्प्राप्त करें मेरे पास इस कोड स्निपेट के साथ jsp है।फ़ाइल प्रकार इनपुट

<form name="AudioFileConversionForm" enctype="multipart/form-data" method="post" > 
Choose File: <input type="file" id="audioFile" name="audioFile"><br> 
<input type="submit" value="upload"> 
</form> 

यह spring में मेरा नियंत्रक है।

public String convertFile(HttpServletRequest request, HttpSession session) { 

    String audioFile = request.getParameter("audioFile"); 
    System.out.println(request.getParameter("audioFile")); 
    System.out.println("Audio File Conversion Successful"); 
} 

मैं फ़ाइल का नाम प्राप्त करने में असमर्थ हूँ, यह null को दर्शाता है। मुझे पता है कि मैं JQuery या जावास्क्रिप्ट का उपयोग कर नाम पुनर्प्राप्त कर सकता हूं, लेकिन मैं उन्हें दोनों का उपयोग नहीं करना चाहता हूं। मैं इसे शुद्ध जावा का उपयोग करना चाहता हूं। क्या कोई मेरी मदद कर सकता है?

+0

आप में से किसी की कोशिश की है कोड, यदि हां, तो हमें बताएं कि क्या आपको समाधान मिला है या नहीं, अगर नहीं, तो अपनी आवश्यकता को आगे समझाएं। – Ravi

+0

मैं एक-एक करके जवाबों का प्रयास कर रहा हूं। मैं निश्चित रूप से नीचे से सबसे उपयुक्त उत्तर अद्यतन अद्यतन करेंगे।उत्तर के लिए सभी को धन्यवाद। – Freakyuser

उत्तर

12

जब आप फ़ाइल अपलोड करते हैं, requestorg.springframework.web.multipart.MultipartHttpServletRequest का उदाहरण है। तो आप इसे अपनी विधि convertFile() में डाल सकते हैं। नीचे देखें:

public String convertFile(HttpServletRequest request, HttpSession session) { 
    // cast request 
    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; 
    // You can get your file from request 
    CommonsMultipartFile multipartFile = null; // multipart file class depends on which class you use assuming you are using org.springframework.web.multipart.commons.CommonsMultipartFile 

    Iterator<String> iterator = multipartRequest.getFileNames(); 

    while (iterator.hasNext()) { 
     String key = (String) iterator.next(); 
     // create multipartFile array if you upload multiple files 
     multipartFile = (CommonsMultipartFile) multipartRequest.getFile(key); 
    } 

    // logic for conversion 
} 

हालांकि मैं (शून्य मूल्य प्राप्त) को पुनः प्राप्त करने के लिए फ़ाइल है कि मैं JSP पेज में चुना है के नाम में असमर्थ हूँ।

---> फ़ाइल नाम प्राप्त करने के लिए आप इसे के रूप में प्राप्त कर सकते हैं:

:

multipartFile.getOriginalFilename(); // get filename on client's machine 
multipartFile.getContentType();  // get content type, you can recognize which kind of file is, pdf or image or doc etc 
multipartFile.getSize()   // get file size in bytes 

फाइल अपलोड काम करने के लिए, आप सुनिश्चित करें कि आप के रूप में नीचे बहुखण्डीय समाधानकर्ता सेम पैदा कर रहे बनाने की जरूरत है

<bean id="multipartResolver" 
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 

    <!-- one of the properties available; the maximum file size in bytes --> 
    <property name="maxUploadSize" value="100000"/> 
</bean> 

संदर्भ: Spring documentation

+0

मैं इस उत्तर को स्वीकार कर रहा हूं, लेकिन क्या मैं आपको 'CommonsMultiPartFile' के लिए उदाहरण बनाने के लिए कोड जोड़ने का सुझाव दे सकता हूं? यह ऑन-दर्स के काम को आसान बनाने में मदद करेगा। या शायद उनके बारे में मार्गदर्शन करने के लिए एक रास्ता या एक लिंक? – Freakyuser

+0

@ फ्रैक्यूसर: मैंने उत्तर और कोड जोड़ा है। :) –

2

उपयोग MultipartFile उपयोगिता और इस

MultipartRequest multipartRequest = (MultipartRequest) request; 
       Map map = multipartRequest.getFileMap(); 
       MultipartFile mfile = null; 
       for (Iterator iter = map.values().iterator(); iter.hasNext();) { 
        mfile = (MultipartFile) iter.next(); 
           String fileName = mfile.getOriginalFilename() 
    } 

कोशिश या आप Apache Commons फाइल अपलोड कर सकते हैं। इस लिंक

की जांच: http://commons.apache.org/fileupload/using.html

2

फ़ाइल का नाम सीधे प्राप्त नहीं किया जा सकता है। आप उपयोग कर सकते हैं अपाचे कॉमन्स FileUpload एपीआई -

// Create a factory for disk-based file items 
FileItemFactory factory = new DiskFileItemFactory(); 

// Create a new file upload handler 
ServletFileUpload upload = new ServletFileUpload(factory); 

// Parse the request 
List /* FileItem */ items = upload.parseRequest(request); 
// Process the uploaded items 
Iterator iter = items.iterator(); 
while (iter.hasNext()) { 
    FileItem item = (FileItem) iter.next(); 

    if (item.isFormField()) { 
     // Process a normal field 
     String name = item.getFieldName(); 
     String value = item.getString(); 

    } else { 
     // Process a file upload field 
    String fileName = item.getName(); 
    // DO further processing 

    } 
} 

अधिक जानकारी -

http://commons.apache.org/fileupload/using.html

यह भी सिर्फ जावा के साथ किया जा सकता है लेकिन स्पष्ट रूप से अधिक कोड की आवश्यकता होगी।

1

लेकिन, सब से पहले, आप कॉमन्स Fileu की आवश्यकता होगी पलोड करें एपीआई, जो आप file.getFieldName() उपयोग करने के लिए प्रपत्र फ़ील्ड का नाम और file.getContentType() प्रदर्शित करने के लिए फ़ाइल और file.getName() की प्रकार प्रदर्शित करने के लिए मदद मिलेगी प्रदर्शित करने के लिए फ़ाइल नाम

public String convertFile(HttpServletRequest request, HttpSession session) { 
    boolean isMultipart = ServletFileUpload.isMultipartContent(request); 
    if(!isMultipart) { 
      out.println("File Not Uploaded"); 
    } 
    else{ 
     FileItemFactory factory = new DiskFileItemFactory(); 
     ServletFileUpload upload = new ServletFileUpload(factory); 
     List items = null; 
     try { 
      items = upload.parseRequest(request); 
      } catch (FileUploadException e) { 
      e.printStackTrace(); 
     } 
     try { 
      FileItem file = (FileItem) items.get(0); 
      out.print("Field Name :"+file.getFieldName()); // Display the field name 
      out.print("Content Type :"+file.getContentType()); // Display Type of File 
      out.print("File Name :"+file.getName()); // Display File Name 
     } catch (Exception e) { 
      out.print(e); 
     } 
    } 
} 
+0

'upload.parseRequest (अनुरोध) 'एक खाली सूची लौटा रहा है। इस प्रकार 'FileItem फ़ाइल = (FileItem) आइटम्स पर एक 'IndexOutOfBoundsException' .get (0)'। – Freakyuser

+0

@ फ्रैक्यूसर 'upload.parseRequest (अनुरोध);' फ़ाइल का सरणी बनाएं। यदि यह 'arrayindexoutofbound' त्रुटि दिखा रहा है, जिसका अर्थ है, यह फ़ाइल अनुरोध प्राप्त कर रहा है। – Ravi

0
Iterator iter = items.iterator(); 
while (iter.hasNext()) { 
    FileItem item = (FileItem) iter.next(); 

    if (item.isFormField()) { 
     // Process a normal field 
     String name = item.getFieldName(); 
     String value = item.getString(); 

    } else { 
     // Process a file upload field 
    String fileName = item.getName(); 
    // DO further processing 

    } 
}