首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >spring文件上传的问题

spring文件上传的问题
EN

Stack Overflow用户
提问于 2011-03-25 08:58:04
回答 2查看 7.5K关注 0票数 2

我有一个简单的表单,有一个上传图像的选项,但是要上传一个文件,我不使用这个方法。

代码语言:javascript
复制
<form:input path="logoData" id="image" type="file" />

相反,我使用的是ajax上传jquery pulgin。问题是upload.parseRequest(请求)在下面的代码中返回null:

代码语言:javascript
复制
@RequestMapping(value = "/upload.htm", method = RequestMethod.POST)
public @ResponseBody String upload(HttpServletRequest request) throws FileUploadException{

    // Create a factory for disk-based file items
    FileItemFactory factory = new DiskFileItemFactory();

    // Create a new file upload handler
    ServletFileUpload upload = new ServletFileUpload(factory);

    // Parse the request
    List<FileItem> items = upload.parseRequest(request);
     System.out.println("====ITEMS====" + items.size());


     System.out.println("----REQUEST---" +request.getParameter("uploadImg"));
        System.out.println("-----SIZE----" +request.getParameterMap().size());
        Map<String, String> map = request.getParameterMap();

        for(Map.Entry<String, String> entry : map.entrySet()){
            System.out.println("----KEY---" + entry.getKey() + "----value---" + entry.getValue());
        }


 // Check that we have a file upload request
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);

    System.out.println("----IS MULTIPART---" +isMultipart);
    return "hello";
}

日志的输出如下:

====ITEMS====0

-请求-无效

-大小

-是多部分的-是真的

我的javascript代码是:

代码语言:javascript
复制
new AjaxUpload('#upload', {
action : my_url+ 'methodName/upload.htm',
name : 'uploadImg',
autoSubmit : true,
responseType: 'html',
onChange: function(file, extension){   },   
onSubmit: function(file, extension) {

},
onComplete: function(file, html) {
    alert(file);
    alert(html);

}

});

是多部分显示为真,但如何获取文件名和如何存储它的。我尝试过一个没有ajax的示例,它使用数据类型CommonsMultipartFile运行得很好。此外,我还在PHP中使用ajaxupload,我将文件名作为$_FILES‘’image‘,但在java中不知道,因为我对java并不熟悉。我在这个网站上遵循了我类似的问题,但没有成功。

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-03-25 09:02:49

你可以把这个缩短一点。您需要一个多部分解析器:

代码语言:javascript
复制
<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="100000"/>
</bean>

然后:

代码语言:javascript
复制
@Controller
public class FileUpoadController {

    @RequestMapping(value = "/form", method = RequestMethod.POST)
    public String handleFormUpload(@RequestParam("file") MultipartFile file) {

        if (!file.isEmpty()) {
            byte[] bytes = file.getBytes();
            // store the bytes somewhere
           return "redirect:uploadSuccess";
       } else {
           return "redirect:uploadFailure";
       }
    }

}
票数 5
EN

Stack Overflow用户

发布于 2012-02-24 10:37:28

代码语言:javascript
复制
@RequestMapping(value = "/imageUpload", method = RequestMethod.POST)
public String handleFormUpload(@RequestParam("file") MultipartFile file) {

    if (!file.isEmpty()) {

            System.out.println("File name:"+ file.getOriginalFilename());
            //byte[] bytes = file.getBytes();
            System.out.println("Content type:"+ file.getContentType());
            String [] contentType = file.getContentType().split("/");
            String fileType = contentType[contentType.length-1];
            System.out.println("File type:"+ fileType);
            System.out.println("File size:"+ (file.getSize()/1024) + "KB");

            String path = context.getRealPath( "/resources/images/");
            System.out.println("Path: " + path);



            InputStream inputStream = null;
            OutputStream outputStream = null;
            try {
                inputStream = file.getInputStream();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                outputStream = new FileOutputStream(path + file.getOriginalFilename());
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            int readBytes = 0;
            byte[] buffer = new byte[8192];
            try {
                while ((readBytes = inputStream.read(buffer, 0, 8192)) != -1) {
                            outputStream.write(buffer, 0, readBytes);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                outputStream.close();
                inputStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            // store the bytes somewhere
            return "theme";
    }
    else
        return "uploadError";

}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5430356

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档