• 表单Content-Type为multipart/form-data时,后台数据的接收


    我们在写form提交表单的时候,后台大多数用request.getParameter的方式来接收前台输入的数据。但如果我们表单中提交的数据包含file文件传输的话,我们需要将Content-Type改为multipart/form-data。这时后台就不能再通过request.getParameter来获取表单的内容了。就需要通过request的输入流request.getInputStream来获取表单内容了。这里记录一种获取表单内容的方式,这种方式既可以获得表单的文本数据也可以获取图片的字节流

    try {
                    String serverRealPath = request.getSession().getServletContext().getRealPath("/");
                    String path=serverRealPath+"saveImg";//文件保存路径
                    DiskFileItemFactory disk=new DiskFileItemFactory();
                    ServletFileUpload sfu=new ServletFileUpload(disk);
                    try {
                        List<FileItem> list=sfu.parseRequest(request);
                        for(FileItem file:list){
                            String fileName=file.getName();
                            if(fileName==null || "".equals(fileName)){
                                DataInputStream dataStream = new DataInputStream(
                                        file.getInputStream());
                                ByteArrayOutputStream output = new ByteArrayOutputStream();
                                byte[] bytes = new byte[1024];
                                int n = 0;
                                while((n=dataStream.read(bytes))!= -1){
                                    output.write(bytes, 0, n);
                                }
                                byte[] newbytes = output.toByteArray();
                                name = new String(newbytes,"UTF-8");
                                
                            }else{
                                LOGGER.info("进入文件上传方法");
                                fileName=fileName.substring(fileName.lastIndexOf("\")+1);
                                String extName=fileName.substring(fileName.lastIndexOf("."));//获取扩展.xxx
                                String newName=tarr_id+extName;//新名称
                                InputStream is = file.getInputStream();// 获取fileItem中的上传文件的输入流
                                FileOutputStream fos = new FileOutputStream(path+ File.separator + newName);// 创建一个文件输出流
                                byte buffer[] = new byte[1024];// 创建一个缓冲区
                                int length = 0;// 判断输入流中的数据是否已经读完的标识
                                while ((length = is.read(buffer)) > 0) {// 循环将输入流读入到缓冲区当中,(len=in.read(buffer))>0就表示in里面还有数据
                                    fos.write(buffer, 0, length);// 使用FileOutputStream输出流将缓冲区的数据写入到指定的目录(savePath + "\" +filename)当中
                                }
                                //按照id生存新的文件名称
                                filePath=newName;
                                LOGGER.info("file1_path的值为:"+newName);
                                is.close();// 关闭输入流
                                fos.close();// 关闭输出流
                                file.delete();// 删除处理文件上传时生成的临时文件
                            }
                            
                        }
                    } catch (Exception e) {
                        LOGGER.error("文件读取失败!");
                        e.printStackTrace();
                    }
                } catch (Exception e) {
                    LOGGER.error("文件上传失败!");
                    e.printStackTrace();
                }

    这里可以将request内容转为一个FileItem的list,然后可以遍历这个list来获取表单的内容,如果其中包含文件的话,file.getName是可以获取文件名的,如果getName的值为空的话,就说明这个不是文件。然后可以通过file.getInputStream获取其输入流,然后将其输出成ByteArrayOutputStream 字节输出流,就可以获取内容。

  • 相关阅读:
    【SVN解决代码提交冲突】https://www.cnblogs.com/aaronLinux/p/5521844.html
    查询有2门及以上不及格科目的学生姓名及其平均成绩
    【Python】split
    【Python】文件处理
    【robotframework】打开浏览器提示:NoSuchWindowException: Message: Unable to get browser
    定位到新窗口
    8月1号
    【定位】https://blog.csdn.net/cyjs1988/article/details/76284289
    【Robotframework】脚本跑完后自动发送邮件
    jQuery Mobile Data 属性
  • 原文地址:https://www.cnblogs.com/qinglangyijiu/p/10696178.html
Copyright © 2020-2023  润新知