ServletFileUpload 简单使用

Posted snakejia

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ServletFileUpload 简单使用相关的知识,希望对你有一定的参考价值。

1 JSP页面

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">


<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>文件管理</title>
    <script type="text/javascript" src="${pageContext.request.contextPath}/base/easyui/jquery.min.js" ></script>
</head>
<body  class="easyui-layout">

    <form method="post" action="http://110.110.11.244:18080/picserver/rest/testupload/processfiles" enctype="multipart/form-data" >
        <input type="file" name="file1" ID="file1"/>  <br/>
        <input type="file" name="file2" ID="file2"/>  <br/>
        <input type="submit" id="fileSubmit" name="Submit" value="上传" />
    </form> 

</div>
</body>
</html>

2.SpringMVC的Controller

package com.tsjsr.controller;

import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import magick.ImageInfo;
import magick.MagickImage;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.im4java.core.ConvertCmd;
import org.im4java.core.IMOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.tsjsr.model.ConfigInfo;
import com.tsjsr.model.FileInfo;
import com.tsjsr.model.FolderInfo;
import com.tsjsr.service.ConfigInfoServiceI;
import com.tsjsr.service.FileInfoServiceI;
import com.tsjsr.service.FolderInfoServiceI;
import com.tsjsr.util.DateHelper;

/**
 * 上传图片业务层
 */
@SuppressWarnings("serial")
@Controller
@RequestMapping("/testupload")
public class TestUploadController extends BaseController {

    
    @RequestMapping(value = "/testupload")
    public String heguishow(HttpServletRequest request, HttpServletResponse response,
            Model model) {
        return "testupload";
    }

    /**
     * http://110.110.11.244:18080/picserver/rest/testupload/processfiles
     * @throws IOException
     * @throws ServletException
     */
    @RequestMapping(value = "/processfiles", method = RequestMethod.POST)
    public String ProcessFiles(HttpServletRequest request,
            HttpServletResponse responser) throws IOException {
        DiskFileItemFactory factory = new DiskFileItemFactory();
        factory.setRepository(new File("E:/temp"));
        factory.setSizeThreshold(10*1024* 1024);
        ServletFileUpload upload = new ServletFileUpload(factory);
        try {
            List<FileItem> list = (List<FileItem>) upload.parseRequest(request);
            for (FileItem item : list) {
                // 获取表单的属性名字
                if (item.isFormField()) {
                    System.out.println(item.getFieldName());
                } else {
                    //打印文件名
                    System.out.println(item.getName());
                    InputStream in = item.getInputStream();
                    File srcFile = new File("E:/temp/"+item.getName());
                    OutputStream out = new FileOutputStream(srcFile);
                    byte[] buf = new byte[1024];
                    int length = 0;
                    while ((length = in.read(buf)) != -1) {
                        out.write(buf, 0, length);
                    }
                    in.close();
                    out.close();
                }           
            }
        } catch (FileUploadException e) {
            e.printStackTrace();
        } catch (Exception e) {
        }
        return null;
    }
}

3.jsp页面选择上传的文件

技术分享图片

4.上传结果

技术分享图片

其中,upload_6564e53e_e7f2_41e6_afd5_c6f9aa4f975b_00000001.tmp文件,是因为其中一个文件的大小,超过了DiskFileItemFactory的设置(代码中设置的是10M)

以上是关于ServletFileUpload 简单使用的主要内容,如果未能解决你的问题,请参考以下文章

文件上传: FileItem类ServletFileUpload 类DiskFileItemFactory类

Apache FileUpload 不适用于 springboot:ServletFileUpload.parseRequest 始终为空

Java 文件上传 MultipartFile与ServletFileUpload

ServletFileUpload.isMultipartContent(request)作用

完成一个简单的时间片轮转多道程序内核代码

Linux内核分析:完成一个简单的时间片轮转多道程序内核代码