我不断收到 MalformedStreamException
我正在使用 Java 8 运行 tomcat 9
客户端正在发送分段文件上传(图片和图片名称)
这是函数签名:
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RequestMapping(value = "/fileupload", headers=("content-type=multipart/*"), method = RequestMethod.POST)
public ResponseEntity<String> handleFileUpload(
@RequestParam("file") MultipartFile file,
@RequestParam("pictureId") String pictureId
Run Code Online (Sandbox Code Playgroud)
这是Spring Dispatcher-servlet.xml中的multipartResolver配置
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- max upload size in bytes -->
<property name="maxUploadSize" value="20971520" /> <!-- 20MB -->
<!-- max size of file in memory (in bytes) -->
<property name="maxInMemorySize" value="1048576" /> <!-- 1MB -->
Run Code Online (Sandbox Code Playgroud)
提交多部分的代码使用 android volley:
SimpleMultiPartRequest …Run Code Online (Sandbox Code Playgroud) 使用Commons FileUpload 1.2.1将大型(> 300MB)文件上传到servlet时,我得到OutOfMemoryErrors.这看起来很奇怪,因为使用DiskFileItem的全部意义是防止(可能很大的)文件驻留在内存中.我使用的默认大小阈值为10KB,所以应该将所有内容加载到堆中,对吧?这是部分堆栈跟踪:
java.lang.OutOfMemoryError
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(FileInputStream.java:177)
at org.apache.commons.fileupload.disk.DiskFileItem.get(DiskFileItem.java:334)
at org.springframework.web.multipart.commons.CommonsMultipartFile.getBytes(CommonsMultipartFile.java:114)
Run Code Online (Sandbox Code Playgroud)
为什么会这样?我缺少一些配置吗?除了增加堆大小之外,还有什么提示/技巧可以避免这种情况?
我真的不应该增加我的堆,因为理论上应该从这个操作加载到内存中的最多是10KB多一点.另外,我的堆max(-Xmx)已设置为1GB,应该足够了.
我知道如何使用Primefaces或使用Tomahawk进行文件上传,但是,我正在尝试使用Apache Commons FileUpload进行文件上传,到目前为止,我有一些路障.即使我的表单使用multipart/form-data,当我提交表单时,内容类型也会变为application/x-www-form-urlencoded.这是我的代码
<h:body>
<h:form enctype="multipart/form-data">
Upload File
<input type="file" name="file"/>
<p:commandButton value="Submit" action="#{viewBean.submit}"/>
</h:form>
</h:body>
Run Code Online (Sandbox Code Playgroud)
这是我的 ViewBean
@ManagedBean
@ViewScoped
public class ViewBean implements Serializable {
public void submit() {
String url = "/FileUploadServlet";
FacesContext context = FacesContext.getCurrentInstance();
try {
String contentType = context.getExternalContext().getRequestContentType();
context.getExternalContext().dispatch(url);
} catch (Exception e) {
logger.log(Level.SEVERE, "Exception when calling Servlet", e);
} finally {
context.responseComplete();
}
}
}
Run Code Online (Sandbox Code Playgroud)
因此,当我尝试打印上面的内容类型时,它显示出来了application/x-www-form-urlencoded.如果我把它放到ajax="false"我的p:commandButton,那么该submit()方法甚至没有被调用,但如果我取出enctype="multipart/form-data"(仍然保持ajax="false"),然后 …
有两种方法可以使用Apache FileUpload Library.
上传文件
http://commons.apache.org/fileupload/using.html
和Streaming FileUpload API
http://commons.apache.org/fileupload/streaming.html
它们都很好用,除了流API在处理流之前似乎没有办法检查fileSize.
这是因为Streaming API从根本上不知道文件大小,还是因为我需要手动读取一些标题或其他内容来获取Multipart上传大小?
在gwt Web应用程序中.我必须发送一个文件和一些附加的参数.
在ServerSide上
try {
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iterator = upload.getItemIterator(request);
while (iterator.hasNext()) {
FileItemStream item = iterator.next();
if (item.isFormField()) {
String fieldName=item.getFieldName();
String fieldValue = Streams.asString(item.openStream());
System.out.println(" chk " +fieldName +" = "+ fieldValue);
} else {
stream = item.openStream();
fileName = item.getName();
mimetype = item.getContentType();
int c;
while ((c = stream.read()) != -1) {
System.out.print((char) c);
}
}
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.println("out of try");
ByteArrayOutputStream output = …Run Code Online (Sandbox Code Playgroud) Apache Commons File Upload包是否提供通用接口来multipart/form-data通过InputStream,附加Array<Byte>或通过任何其他通用流接口流解析块?
我知道他们有一个流API,但这个例子只显示了如何通过ServletFileUpload这个,我认为必须具体Servlet.
如果没有,JVM中是否有任何其他可供选择的框架可以让你做到这一点?可悲的是,我使用的框架Spray.io似乎没有提供这样做的方法.
我正在尝试获取他们上载的用户文件,但是当您选择文件并提交时,该错误就会出现。
错误:
org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/x-www-form-urlencoded
at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:947)
at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:310)
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:334)
at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:115)
at com.example.HttpRequest.doPost(HttpRequest.java:153)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) …Run Code Online (Sandbox Code Playgroud) 我正在使用Apache公共文件上载库和Netbeans 6.8 + Glassfish.我正在尝试将当前上载路径更改为位于servlet的当前上下文路径中,如下所示:WEB-INF/upload
所以我写道:
File uploadedFile = new File("WEB-INF/upload/"+fileName);
session.setAttribute("path",uploadedFile.getAbsolutePath());
item.write(uploadedFile);
Run Code Online (Sandbox Code Playgroud)
但是我注意到库将上传的文件保存到glassfish文件夹中,这是我打印上传文件的绝对路径时得到的:
C:\Program Files\sges-v3\glassfish\domains\domain1\WEB-INF\upload\xx.rar
Run Code Online (Sandbox Code Playgroud)
我的问题 :
我使用CommonsMultipartResolver进行文件上传.
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
<!-- specify maximum file size in bytes -->
<property name="maxUploadSize" value="100000"/>
</bean
Run Code Online (Sandbox Code Playgroud)
我希望能够在运行时更改其属性maxUploadSize(以便管理员可以更改大小).请问最好的方法是什么?
java spring file-upload spring-mvc apache-commons-fileupload
使用裸servlet的doPost,当文件上载开始时,立即调用doPost.然后,我可以使用commons FileItemIterator从请求对象中流式传输文件.
使用Spring MVC,我似乎无法得到控制的方法来触发,直到后的文件(S)已全部由服务器,这是不理想的接收.
我希望我的servlet/controller方法尽可能多地处理文件,并在上传中断时执行一些回滚操作.我目前无法使用Spring MVC做到这一点.
public void doPost(HttpServletRequest request, HttpServletResponse res){
//I can immediately stream the response here
}
Run Code Online (Sandbox Code Playgroud)
与
@RequestMapping(value="/uploadFiles", method= RequestMethod.POST)
public @ResponseBody String addFiles(ContentManagerTicket ticket, HttpServletRequest request){
//I can't do anything until the files are received - whether i use a HttpServletRequset or MultiPartFile
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?谢谢!