enctype="multipart/form-data"的表单无法获取表单中除了type=file以外的其他参数 commons-fileupload 获取除file外其他参数

大家会发现,enctype="multipart/form-data"的表单提交后,在后台无法用传统的request.getParameter获取其他参数

我个人理解是因为每个参数都用流的形式处理了,需要特别的方式才能获取

本文使用commons-fileupload 获取除file外其他参数

HTML代码:

                        <form id="uploadFileForm" action="UploadServlet" target="hiddenFrame" method="post" enctype="multipart/form-data">
                            <input id="fileInput" type="file" onchange="uploadFile();" name="upload" >
                            <input type="hidden" value="/image/" name="directory">
                        </form>

Servlet代码:

package UI.FileManage;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class UploadServlet extends HttpServlet {

    @SuppressWarnings("unchecked")
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String errorMessage = null;
        try {
            DiskFileItemFactory fac = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(fac);
            upload.setHeaderEncoding("utf-8");
            List fileList = null;
            fileList = upload.parseRequest(request);

            String dir = ((FileItem)fileList.get(1)).getString();   //获取directory参数
            if (dir == null) {
                dir = "/";
            }
            String savePath = getServletContext().getRealPath("/test" + dir);
            System.out.println("save at:" + savePath);
            File f1 = new File(savePath);
            if (!f1.exists()) {
                f1.mkdirs();
            }
            
            Iterator<FileItem> it = fileList.iterator();
            String name = "";
            while (it.hasNext()) {
                FileItem item = it.next();
                if (!item.isFormField()) {
                    name = item.getName();
                    File file = new File(savePath + File.separatorChar + name);
                    if (file.exists()) {
                        errorMessage = "该文件已经存在!请重命名后再上传。";
                    }
                    item.write(file);
                    item.delete();  //释放输出流
                }
            }
            if (errorMessage == null) {
                response.getWriter().print("<script> parent.finishUploading(1);</script>");
            } else {
                response.getWriter().print("<script> parent.finishUploading(0,'" + errorMessage + "');</script>");
            }
        } catch (Exception exception) {
            response.getWriter().print("<script> parent.finishUploading(0,'上传失败,请重试。');</script>");
        }
    }
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏我和未来有约会

Kit 3D 更新

Kit3D is a 3D graphics engine written for Microsoft Silverlight. Kit3D was inita...

2506
来自专栏芋道源码1024

熔断器 Hystrix 源码解析 —— 断路器 HystrixCircuitBreaker

本文主要基于 Hystrix 1.5.X 版本 1. 概述 2. HystrixCircuitBreaker 3. HystrixCircuitBreaker....

5267
来自专栏码匠的流水账

聊聊NettyConnector的start及shutdown

reactor-netty-0.7.6.RELEASE-sources.jar!/reactor/ipc/netty/NettyConnector.java

841
来自专栏魂祭心

原 canvas绘制clock

4014
来自专栏跟着阿笨一起玩NET

c#实现打印功能

2612
来自专栏飞扬的花生

jsencrypt参数前端加密c#解密

      写程序时一般是通过form表单或者ajax方式将参数提交到服务器进行验证,如何防止提交的请求不被抓包后串改,虽然无法说绝对安全却给非法提交提高了难度...

3849
来自专栏一个会写诗的程序员的博客

Spring Reactor 项目核心库Reactor Core

Non-Blocking Reactive Streams Foundation for the JVM both implementing a Reactiv...

2102
来自专栏张善友的专栏

Mix 10 上的asp.net mvc 2的相关Session

Beyond File | New Company: From Cheesy Sample to Social Platform Scott Hansel...

2517
来自专栏Ceph对象存储方案

Luminous版本PG 分布调优

Luminous版本开始新增的balancer模块在PG分布优化方面效果非常明显,操作也非常简便,强烈推荐各位在集群上线之前进行这一操作,能够极大的提升整个集群...

3035
来自专栏落花落雨不落叶

canvas画简单电路图

58511

扫码关注云+社区