码迷,mamicode.com
首页 > 其他好文 > 详细

ServletFileUpload 简单使用

时间:2018-01-12 15:30:41      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:jquer   str   alt   springmvc   rcfile   exce   info   submit   art   

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 简单使用

标签:jquer   str   alt   springmvc   rcfile   exce   info   submit   art   

原文地址:https://www.cnblogs.com/snakejia/p/8275875.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!