Kev*_*vMo 5 java proxy gwt servlets pandastream
我正试图用我的GWT应用程序使用Panda.我可以使用直接将视频上传到我的熊猫服务器
POST MY_PANDA_SERVER/videos/MY_VIDEO_ID/upload
Run Code Online (Sandbox Code Playgroud)
但是,我想把我的熊猫服务器隐藏在我的J2EE(glassfish)服务器之后.我想实现这个目标:
理想情况下,我希望永远不会将文件存储在J2EE服务器上,而只是将其用作代理来访问熊猫服务器.
Commons FileUpload很不错,但在你的情况下还不够.在提供文件项(和流)之前,它将在内存中解析整个主体.你对个别物品不感兴趣.您基本上只想透明地将请求主体从一个流传输到另一个端,而无需改变它或以任何方式将其存储在内存中.FileUpload只会将请求主体解析为一些"可用的"Java对象,而HttpClient只会根据这些Java对象再次创建相同的请求主体.这些Java对象也会消耗内存.
你不需要一个库(或者它必须是Commons IO来替换for
使用oneliner 的循环IOUtils#copy()
).只需基本的Java .NET和IO API即可.这是一个启动示例:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
URLConnection connection = new URL("http://your.url.to.panda").openConnection();
connection.setDoOutput(true); // POST.
connection.setRequestProperty("Content-Type", request.getHeader("Content-Type")); // This one is important! You may want to check other request headers and copy it as well.
// Set streaming mode, else HttpURLConnection will buffer everything.
int contentLength = request.getContentLength();
if (contentLength > -1) {
// Content length is known beforehand, so no buffering will be taken place.
((HttpURLConnection) connection).setFixedLengthStreamingMode(contentLength);
} else {
// Content length is unknown, so send in 1KB chunks (which will also be the internal buffer size).
((HttpURLConnection) connection).setChunkedStreamingMode(1024);
}
InputStream input = request.getInputStream();
OutputStream output = connection.getOutputStream();
byte[] buffer = new byte[1024]; // Uses only 1KB of memory!
for (int length = 0; (length = input.read(buffer)) > 0;) {
output.write(buffer, 0, length);
output.flush();
}
output.close();
connection.getInputStream(); // Important! It's lazily executed.
}
Run Code Online (Sandbox Code Playgroud)