में पूरी फ़ाइल भंडारण के बिना सिस्टम A-> बी> सेल्सियस के बीच फ़ाइल भेजा जा रहा है मैं 3 अलग वसंत वेब अनुप्रयोगोंबी
है- एक का उपयोग करता है वसंत 4.x
- बी वसंत 3.2 का उपयोग करता है .0
- सी वसंत 4.x का उपयोग करता
बी और सी फ़ाइलों को अपलोड करने के लिए
बाकी नियंत्रकों को उजागर करता है- एक पढ़ता
- बी फ़ाइल सामग्री पढ़ने के लिए किसी भी आवश्यकता के बिना सी लिए अनुरोध भेजता है यह बी करने के लिए फ़ाइल और अपलोड
- और फिर सी जो भी वह फ़ाइल के साथ चाहता है करता है।
तो प्रवाह होगा A-> बी> सी
मेरा प्रश्न है - इस तरह से सेटअप बी लिए यह संभव है ताकि बी पूरी की दुकान नहीं होगा मेमोरी में फ़ाइल करें, लेकिन इनकमिंग स्ट्रीम पढ़ेगी और इसे सी पर अग्रेषित करेगी?
मैं क्या करने में कामयाब रहे है: एक
public void sendFileFromA() throws FileNotFoundException {
final InputStream fis = new FileInputStream(new File("someFile"));
final RequestCallback requestCallback = new RequestCallback() {
@Override
public void doWithRequest(final ClientHttpRequest request) throws IOException {
request.getHeaders().add("Content-type", "application/octet-stream");
IOUtils.copy(fis, request.getBody());
}
};
final RestTemplate restTemplate = new RestTemplate();
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setBufferRequestBody(false);
restTemplate.setRequestFactory(requestFactory);
final HttpMessageConverterExtractor<String> responseExtractor = new HttpMessageConverterExtractor<>(
String.class, restTemplate.getMessageConverters());
restTemplate.execute("http://b_url/upload", HttpMethod.POST, requestCallback, responseExtractor);
}
बी
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody String handleFileUpload(HttpServletRequest request) throws IOException {
final ServletInputStream input = request.getInputStream();
final RequestCallback requestCallback = new RequestCallback() {
@Override
public void doWithRequest(final ClientHttpRequest request) throws IOException {
request.getHeaders().add("Content-type", "application/octet-stream");
try (OutputStream body = request.getBody()) {
IOUtils.copy(input, body);
}
}
};
final RestTemplate restTemplate = new RestTemplate();
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setBufferRequestBody(false);
restTemplate.setRequestFactory(requestFactory);
final HttpMessageConverterExtractor<String> responseExtractor = new HttpMessageConverterExtractor<>(
String.class, restTemplate.getMessageConverters());
restTemplate.execute("http://c_url/upload", HttpMethod.POST, requestCallback, responseExtractor);
return "success";
}
सी
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody String handleFileUpload(HttpServletRequest request) throws IOException {
ServletInputStream input = request.getInputStream();
try (BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream("zibiTest"))) {
IOUtils.copy(input, output);
}
return "success";
}
मैं बी
का उपयोग करके फ़ाइलों को आसानी से प्रतिलिपि बना सकता है। ऐसे समाधान के साथ हम ए को रोकने के दौरान ए को रोकने की कोशिश कर सकते हैं, बी और सी को त्रुटि के बारे में अधिसूचित किया जाना चाहिए, लेकिन कभी-कभी ऐसा होता है कि त्रुटि संदेश सी तक नहीं पहुंचता है - यह सॉकेट टाइमआउट अपवाद के साथ बंद हो जाता है, कोई विचार क्यों होता है और इसे ठीक से कैसे कार्यान्वित किया जाए?
क्या यह एक वैध दृष्टिकोण है या इसे बेहतर तरीके से संभाला जा सकता है?
आदेश में एक फ़ाइल आप लोड करने के लिए की जरूरत है पढ़ने के लिए "मेरी जानकारी समस्या यह है कि केवल आप को देखने के लिए सीमित है, तो मैं एकमात्र समाधान के लिए एक मध्यम और समाधान ही नहीं हो सकता है" स्मृति के लिए बी में फ़ाइल प्राप्त करने वाली व्हील फ़ाइल पहले से ही स्मृति में है। – reos
फ़ाइल को बी में एक थ्रेड में एक पाइप में लिखें, फिर उसे सी में भेजने से पहले इसे (बी में भी) में पढ़ें। Java.nio.channels.Pipe देखें। सी यह जानना चाहता है कि यह कितना डेटा प्राप्त करने वाला है (http शीर्षलेख)। –
@reos अच्छी तरह से, कोई फ़ाइल का हिस्सा पढ़ सकता है, इसके साथ कुछ कर सकता है, फिर अगला भाग पढ़ें, ... – Henry