2013-08-22 7 views
9

में कनवर्ट करने के लिए कैसे मैं BASE64 एन्कोडेड स्ट्रिंग (एन्कोडेड बाइट्स) के रूप में छवि प्राप्त कर रहा हूं और सर्वर पक्ष पर बाइट [] में डीकोड करने के लिए निम्न दृष्टिकोण का उपयोग कर रहा हूं।बाइट सरणी को मल्टीपार्टफाइल

BASE64Decoder decoder = new BASE64Decoder(); 
byte[] decodedBytes = decoder.decodeBuffer(encodedBytes); 

अब मैं इसे ऊपर प्राप्त इस बाइट का उपयोग करके मल्टीपार्टफाइल में परिवर्तित करना चाहता हूं?

क्या बाइट [] से org.springframework.web.multipart.MultipartFile को परिवर्तित करने का कोई तरीका है ??

उत्तर

15

org.springframework.web.multipart.MultipartFile एक इंटरफ़ेस है इसलिए सबसे पहले आपको इस इंटरफ़ेस के कार्यान्वयन के साथ काम करने की आवश्यकता होगी।

एकमात्र कार्यान्वयन जिसे मैं उस इंटरफ़ेस के लिए देख सकता हूं जिसे आप आउट-ऑफ-द-बॉक्स का उपयोग कर सकते हैं org.springframework.web.multipart.commons.CommonsMultipartFile है। उस कार्यान्वयन के लिए एपीआई here

वैकल्पिक रूप से org.springframework.web.multipart.MultipartFile एक इंटरफ़ेस है, तो आप अपना स्वयं का कार्यान्वयन प्रदान कर सकते हैं और बस अपने बाइट सरणी को लपेट सकते हैं। एक मामूली उदाहरण के रूप में:

/* 
*<p> 
* Trivial implementation of the {@link MultipartFile} interface to wrap a byte[] decoded 
* from a BASE64 encoded String 
*</p> 
*/ 
public class BASE64DecodedMultipartFile implements MultipartFile 
{ 
     private final byte[] imgContent; 

     public BASE64DecodedMultipartFile(byte[] imgContent) 
     { 
      this.imgContent = imgContent; 
      } 

     @Override 
      public String getName() 
     { 
       // TODO - implementation depends on your requirements 
        return null; 
     } 

     @Override 
      public String getOriginalFilename() 
     { 
      // TODO - implementation depends on your requirements 
       return null; 
     } 

     @Override 
     public String getContentType() 
     { 
      // TODO - implementation depends on your requirements 
      return null; 
     } 

     @Override 
     public boolean isEmpty() 
     { 
      return imgContent == null || imgContent.length == 0; 
     } 

     @Override 
     public long getSize() 
     { 
      return imgContent.length; 
     } 

     @Override 
     public byte[] getBytes() throws IOException 
     { 
      return imgContent; 
     } 

     @Override 
     public InputStream getInputStream() throws IOException 
     { 
      return new ByteArrayInputStream(imgContent); 
     } 

     @Override 
     public void transferTo(File dest) throws IOException, IllegalStateException 
     { 
      new FileOutputStream(dest).write(imgContent); 
     } 
    } 
+1

ठंडा ठंडा। धन्यवाद भाई। –

+1

आपके द्वारा बहुत अच्छा समाधान दिया गया है। इस सवाल का जवाब दें और उत्तर कई लोगों के लिए उपयोगी होगा –

+0

'हस्तांतरण में', क्या फ़ाइलऑटपुटस्ट्रीम लेखन के बाद बंद होना चाहिए? – Ascalonian

0

यह उत्तर पहले ही उत्तर दिया गया है। हाल ही में मैं बाइट सरणी ऑब्जेक्ट को multipartfile ऑब्जेक्ट में कनवर्ट करने की आवश्यकता पर काम कर रहा था। इसे प्राप्त करने के दो तरीके हैं।

दृष्टिकोण 1:

जहां FileDiskItem वस्तु का उपयोग करने के लिए इसे बनाने के लिए डिफ़ॉल्ट CommonsMultipartFile का प्रयोग करें। उदाहरण:

Approach 1: 

जहां FileDiskItem वस्तु का उपयोग करने के लिए इसे बनाने के लिए डिफ़ॉल्ट CommonsMultipartFile का प्रयोग करें। उदाहरण:

FileItem fileItem = new DiskFileItem("fileData", "application/pdf",true, outputFile.getName(), 100000000, new java.io.File(System.getProperty("java.io.tmpdir")));    
MultipartFile multipartFile = new CommonsMultipartFile(fileItem); 

दृष्टिकोण 2:

अपने स्वयं के कस्टम बहुखण्डीय फ़ाइल वस्तु बनाएँ और multipartfile को बाइट सरणी कनवर्ट करें।

public class CustomMultipartFile implements MultipartFile { 

private final byte[] fileContent; 

private String fileName; 

private String contentType; 

private File file; 

private String destPath = System.getProperty("java.io.tmpdir"); 

private FileOutputStream fileOutputStream; 

public CustomMultipartFile(byte[] fileData, String name) { 
    this.fileContent = fileData; 
    this.fileName = name; 
    file = new File(destPath + fileName); 

} 

@Override 
public void transferTo(File dest) throws IOException, IllegalStateException { 
    fileOutputStream = new FileOutputStream(dest); 
    fileOutputStream.write(fileContent); 
} 

public void clearOutStreams() throws IOException { 
if (null != fileOutputStream) { 
     fileOutputStream.flush(); 
     fileOutputStream.close(); 
     file.deleteOnExit(); 
    } 
} 

@Override 
public byte[] getBytes() throws IOException { 
    return fileContent; 
} 

@Override 
public InputStream getInputStream() throws IOException { 
    return new ByteArrayInputStream(fileContent); 
} 
} 

यह आप कस्टममल्टीपार्टफ़ाइल ऑब्जेक्ट से ऊपर कैसे उपयोग कर सकते हैं।

String fileName = "intermediate.pdf"; 
CustomMultipartFile customMultipartFile = new CustomMultipartFile(bytea, fileName); 
try { 
customMultipartFile.transferTo(customMultipartFile.getFile()); 

} catch (IllegalStateException e) { 
    log.info("IllegalStateException : " + e); 
} catch (IOException e) { 
    log.info("IOException : " + e); 
} 

इस आवश्यक पीडीएफ बना सकते हैं और स्टोर है कि में

java.io.tmpdir नाम के साथ intermediate.pdf

धन्यवाद होगा।