标签: apache-commons-fileupload

直播意外结束

我不断收到 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)

java spring apache-commons-fileupload android-volley

6
推荐指数
1
解决办法
9906
查看次数

使用Commons FileUpload的DiskFileItem上传大文件时,如何避免OutOfMemoryErrors?

使用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,应该足够了.

java heap servlets out-of-memory apache-commons-fileupload

5
推荐指数
1
解决办法
8123
查看次数

如何让JSF使用Apache Commons FileUpload上传文件

我知道如何使用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"),然后 …

jsf file-upload jsf-2 apache-commons-fileupload

5
推荐指数
1
解决办法
1万
查看次数

在Apache FileUpload Streaming API中读取之前确定文件上载的大小

有两种方法可以使用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上传大小?

java file-upload apache-commons-fileupload

5
推荐指数
1
解决办法
8651
查看次数

为什么这个异常FileItemStream $ ItemSkippedException?

在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)

java file-upload apache-commons-fileupload

5
推荐指数
1
解决办法
2047
查看次数

使用Apache Commons File Upload解析multipart/form-data

Apache Commons File Upload包是否提供通用接口来multipart/form-data通过InputStream,附加Array<Byte>或通过任何其他通用流接口流解析块?

我知道他们有一个流API,但这个例子只显示了如何通过ServletFileUpload这个,我认为必须具体Servlet.

如果没有,JVM中是否有任何其他可供选择的框架可以让你做到这一点?可悲的是,我使用的框架Spray.io似乎没有提供这样做的方法.

java jvm scala multipartform-data apache-commons-fileupload

5
推荐指数
0
解决办法
2899
查看次数

请求不包含多部分/表单数据或多部分/混合流,内容类型标头为application / x-www-form-urlencoded

我正在尝试获取他们上载的用户文件,但是当您选择文件并提交时,该错误就会出现。

错误:

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)

java http apache-commons-fileupload

5
推荐指数
1
解决办法
1万
查看次数

如何使用Apache Common fileupload为上传的文件设置"上下文路径"的路径?

我正在使用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)

我的问题 :

  • 如何强制公共文件上载将上传的文件保存在相对于当前servlet路径的路径中,所以我不需要指定整个路径?这可能吗 ?

java servlets apache-commons-fileupload

4
推荐指数
1
解决办法
2万
查看次数

在运行时更改CommonsMultipartResolver的maxUploadSize

我使用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

4
推荐指数
1
解决办法
3896
查看次数

Spring MVC文件上传控制器 - 我希望上传开始后立即调用控制器

使用裸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)

有任何想法吗?谢谢!

spring-mvc apache-commons-fileupload

4
推荐指数
1
解决办法
3160
查看次数