我有一个简单的Web应用程序,只有一个Servlet接受数据文件并将其保存到服务器。我正在使用“apache commons FileUpload”库。文件上传在我的本地服务器上工作正常(我正在使用Glassfish作为我的Dev和Prod服务器)。我可以上传任何大小的文件。这是我的记忆信息:
-XX:MaxPermSize=512m
-Xmx1024m
这是我的servlet的代码:
...
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
log.info("Let's try to do something with your request");
getMultipartRequestFile(request);
}
// Handling uploaded file with commons-fileupload library
private void getMultipartRequestFile(HttpServletRequest request){
if(!ServletFileUpload.isMultipartContent(request)) // if not is multi part then exit this function
return;
FileItemFactory factory = new DiskFileItemFactory(); // Create a factory for file items
ServletFileUpload upload = new ServletFileUpload(factory); // Create a new file upload handler
//Create a progress listener
ProgressListener progressListener = new ProgressListener(){
private long megaBytes = -1;
public void update(long pBytesRead, long pContentLength, int pItems) {
long mBytes = pBytesRead / 1000000;
if (megaBytes == mBytes) {
return;
}
megaBytes = mBytes;
if (pContentLength == -1) {
log.info("So far, " + dec.format(pBytesRead/1024.0/1024.0) + " have been read.");
} else {
log.info("So far, " + dec.format(pBytesRead/1024.0/1024.0) + "\tof " + dec.format(pContentLength/1024.0/1024.0));
}
}
};
upload.setProgressListener(progressListener);
// Parse the request
try {
List items = upload.parseRequest(request);
// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
processFormField(item);
} else {
processUploadedFile(item);
}
}
}
catch (FileUploadException e) {
e.printStackTrace();
}
}
// this is where all magic with the file will happen
private void processUploadedFile(FileItem item){
if (!item.isFormField()) {
String fieldName = item.getFieldName();
String fileName = item.getName();
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
log.info("Field name: " + fieldName + "\nFile Name: " + fileName + "\nContent type: " +contentType+ "\nSize: " + dec.format(sizeInBytes/1024.0/1024.0));
// write uploaded file to hdd
File uploadedFile = new File(FILES_FOLDER + fileName);
try {
item.write(uploadedFile);
log.info("File location: " + FILES_FOLDER + fileName);
} catch (Exception e) {
log.warning("File cannot be writtet to the disc");
e.printStackTrace();
}
}
}
...
这是输出错误:
[#|2009-07-13T22:19:31.822+0000|INFO|sun-appserver2.1|com.athena.video.upload.server.Fileuploader|_ThreadID=18;_ThreadName=httpSSLWorkerThread-8080-0;|Let's try to do something with
your request|#]
[#|2009-07-13T22:19:31.823+0000|INFO|sun-appserver2.1|com.athena.video.upload.server.Fileuploader|_ThreadID=18;_ThreadName=httpSSLWorkerThread-8080-0;|So far, 0.00 mb of 87.64 mb|#]
[#|2009-07-13T22:19:33.403+0000|INFO|sun-appserver2.1|com.athena.video.upload.server.Fileuploader|_ThreadID=18;_ThreadName=httpSSLWorkerThread-8080-0;|So far, 0.95 mb of 87.64 mb|#]
[#|2009-07-13T22:19:34.109+0000|INFO|sun-appserver2.1|com.athena.video.upload.server.Fileuploader|_ThreadID=18;_ThreadName=httpSSLWorkerThread-8080-0;|So far, 1.91 mb of 87.64 mb|#]
[#|2009-07-13T22:19:34.739+0000|INFO|sun-appserver2.1|com.athena.video.upload.server.Fileuploader|_ThreadID=18;_ThreadName=httpSSLWorkerThread-8080-0;|So far, 2.86 mb of 87.64 mb|#]
[#|2009-07-13T22:19:35.371+0000|INFO|sun-appserver2.1|com.athena.video.upload.server.Fileuploader|_ThreadID=18;_ThreadName=httpSSLWorkerThread-8080-0;|So far, 3.82 mb of 87.64 mb|#]
[#|2009-07-13T22:19:35.989+0000|INFO|sun-appserver2.1|com.athena.video.upload.server.Fileuploader|_ThreadID=18;_ThreadName=httpSSLWorkerThread-8080-0;|So far, 4.77 mb of 87.64 mb|#]
[#|2009-07-13T22:19:36.938+0000|WARNING|sun-appserver2.1|javax.enterprise.system.stream.err|_ThreadID=18;_ThreadName=httpSSLWorkerThread-8080-0;_RequestID=99b72ec5-047c-4a86-9160-994ea31848a2;|org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:367)
at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
at com.athena.video.upload.server.Fileuploader.getMultipartRequestFile(Fileuploader.java:109)
at com.athena.video.upload.server.Fileuploader.doPost(Fileuploader.java:47)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
at org.apache.catalina.core.ApplicationFilterChain.servletService(ApplicationFilterChain.java:427)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:315)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:287)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:218)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:94)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:98)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:222)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:587)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1096)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:166)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:587)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1096)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:288)
at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.invokeAdapter(DefaultProcessorTask.java:647)
at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.doProcess(DefaultProcessorTask.java:579)
at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.process(DefaultProcessorTask.java:831)
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.executeProcessorTask(DefaultReadTask.java:341)
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:263)
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:214)
at com.sun.enterprise.web.portunif.PortUnificationPipeline$PUTask.doTask(PortUnificationPipeline.java:380)
at com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:265)
at com.sun.enterprise.web.connector.grizzly.ssl.SSLWorkerThread.run(SSLWorkerThread.java:106)
Caused by: org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly
at org.apache.commons.fileupload.MultipartStream$ItemInputStream.makeAvailable(MultipartStream.java:983)
at org.apache.commons.fileupload.MultipartStream$ItemInputStream.read(MultipartStream.java:887)
at java.io.InputStream.read(InputStream.java:85)
at org.apache.commons.fileupload.util.Streams.copy(Streams.java:94)
at org.apache.commons.fileupload.util.Streams.copy(Streams.java:64)
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:362)
... 33 more
|#]
[#|2009-07-13T22:19:37.839+0000|SEVERE|sun-appserver2.1|javax.enterprise.system.container.web|_ThreadID=19;_ThreadName=httpSSLWorkerThread-8080-1;_RequestID=528b60b2-7083-4f27-a1de-0c90df2a34f1;|WEB0777: Unblocking keep-alive exception
java.lang.IllegalStateException: PWC4662: Request header is too large
at org.apache.coyote.http11.InternalInputBuffer.fill(InternalInputBuffer.java:740)
at org.apache.coyote.http11.InternalInputBuffer.parseRequestLine(InternalInputBuffer.java:442)
at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.parseRequest(DefaultProcessorTask.java:694)
at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.doProcess(DefaultProcessorTask.java:577)
at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.process(DefaultProcessorTask.java:831)
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.executeProcessorTask(DefaultReadTask.java:341)
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:263)
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:214)
at com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:265)
以下是我还尝试添加到我的domain.xml中的内容:
<http-listener acceptor-threads="1" address="0.0.0.0" blocking-enabled="false" default-virtual-server="server" enabled="true" family="inet" id="http-listener-1" port="8080" security-enabled="false" server-name="" xpowered-by="true">
<property name="maxPostSize" value="0"/> <!-- 0 means no max -->
<property name="proxiedProtocols" value="ws/tcp"/>
</http-listener>
知道为什么会发生这种情况
答案 0 :(得分:2)
检查两者上的glassfish服务器的版本。我有同样的问题,并发现apache commons文件上传在glassfish服务器3.1.2上有问题。当我升级到服务器版本3.1.2.2(版本5)时,文件上载功能正常。
答案 1 :(得分:1)
java.lang.IllegalStateException: PWC4662: Request header is too large
您需要配置HTTP连接器以使用更大的缓冲区。
HTTP侦听器的设置中应该有一个参数“maxPostSize”。
答案 2 :(得分:1)
我只能推测。您是否在两台服务器上使用相同版本的库/容器?你可以嗅探你的文件上传(使用apache工具 - 不记得名字)并检查它是否正确上传?您可以手动获取servlet中上传的主体并检查servlet过滤器/默认请求解析器是否没有损坏?
我还找到了这个issue here。基本上它表明上传期间的套接字超时导致异常。