JSP页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><form action="UploadServlet" enctype="multipart/form-data" method="post">用户姓名:<input type="text" name="userName" /><br/>上传头像:<input type="file" name="userPhoto" /><br/><input type="submit" value="提交"></form></body>
</html>

Servlet代码:

package com.gy.servlet;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.jspsmart.upload.File;
import com.jspsmart.upload.SmartUpload;public class UploadServlet extends HttpServlet {/*** Constructor of the object.*/public UploadServlet() {super();}/*** Destruction of the servlet. <br>*/public void destroy() {super.destroy(); // Just puts "destroy" string in log// Put your code here
    }/*** The doGet method of the servlet. <br>** This method is called when a form has its tag value method equals to get.* * @param request the request send by the client to the server* @param response the response send by the server to the client* @throws ServletException if an error occurred* @throws IOException if an error occurred*/public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}/*** The doPost method of the servlet. <br>** This method is called when a form has its tag value method equals to post.* * @param request the request send by the client to the server* @param response the response send by the server to the client* @throws ServletException if an error occurred* @throws IOException if an error occurred*/public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");request.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");PrintWriter out = response.getWriter();try{SmartUpload su = new SmartUpload();//创建SmartUpload对象su.setCharset("utf-8");//设置编码su.initialize(this.config,request,response);//初始化设置             su.setAllowedFilesList("gif,bmp,jpg");//设置允许上传的文件扩展名su.setMaxFileSize(2*1024*1024);//设置单个文件允许最大长度(单位:字节)su.setTotalMaxFileSize(10*1024*1024);//设置上传文件的总大小(单位:字节)su.upload();//将文件数据上传//注意:使用SmartUpload对象获取request这句代码要放在upload()方法后面,否则拿不到数据out.print(su.getRequest().getParameter("userName"));File f = su.getFiles().getFile(0);//获取第一个上传文件String savePath = "upload\\";savePath += f.getFileName();//组装存储路径f.saveAs(savePath);//将文件保存到指定位置out.println("上传成功!");}catch(Exception ex){ex.printStackTrace();out.println("上传异常:"+ex.getMessage());}out.flush();out.close();}private ServletConfig config;/*** Initialization of the servlet. <br>** @throws ServletException if an error occurs*/public void init(ServletConfig config) throws ServletException {// Put your code herethis.config = config;}}

SmartUpload实现文件上传时file和表单文本同时提交的问题相关推荐

  1. jqm文件上传,上传图片,jqm的表单操作,jqm的ajax的使用,jqm文件操作大全,文件操作demo

    最近在论坛中看到,在使用html5中上传图片或文件,出现各种问题.这一方面,我也一直没有做过,今天就抽出了一点时间来学习一下.现在的示例已经ok了,我就给大家分享一下,希望对大家有帮助. 好吧,我们先 ...

  2. 【一文学会文件上传】SpringBoot+form表单实现文件上传

    唠嗑部分 平时我们在项目过程中,往往会遇到这种情况,比如:我的用户应该有一个头像,那就涉及到文件上传,那么文件应该如何存储呢? 这就会有很多方式 1.最简单的就是存在服务器上,这就要考虑到服务器的磁盘 ...

  3. php中的file_upload,PHP文件上传(PHP file upload)

    PHP文件上传(PHP file upload) 我正在尝试使用php将文件上传到我的服务器,将它们保存到我的mysql数据库中,但我无法让它工作,这是我正在使用的脚本,我相信它与"$ _F ...

  4. Struts2学习笔记(十六) 文件上传(File Upload)

    使用jsp/Servlet实现文件上传 在我们的web应用中,文件上传是一项非常常见的功能.以前我学习php的时候也用php写过文件上传,不过php中实现起来比较方便,只需要从全局变量$_FILES中 ...

  5. DVWA靶机-文件上传漏洞(File Upload)

    DVWA靶机-文件上传漏洞(File Upload) 文件上传漏洞的四个等级,low,medium,high,impossible,今天我们针对于不同的等级进行基于文件上传漏洞的攻击 DVWA靶机-暴 ...

  6. 解决稻香老农无组件上传类在文件上传时当文件名中含中文单双引号取不到正确的文件名的方案

    今天完成了对稻香老农的最新版本无组件上传类V2.1的修改. 解决了文件上传时当文件名中含中文单双引号取不到正确的文件名的问题. 参考了无惧上传类,在此感谢稻香老农和梁无惧两位老师,谢谢他们无偿的奉献了 ...

  7. 文件上传时快速生成1G、10G大小的空文件

    文章目录 前言 1.Linux系统 2.windows系统 前言 本文是由于公司测试人员测试文件上传时, 问我该如何创建指定大小的大文件 比如1G.10G等大小的文件 本文讲述了linux系统.win ...

  8. 文件上传时,文件太大会有几种被限制的情况

    文件上传时,文件太大会有几种被限制的情况 ​ 近期在维护一个项目时,商户有反馈,在新增用户时会直接卡住并报错,返回一个异常,应用层没有日志,最后排查出来是nginx限制了请求的最大值. ​ 那么整个链 ...

  9. php文件上传css,CSS_文件上传input file简便美化方案(css),文件上传input在各个浏览器里 - phpStudy...

    文件上传input file简便美化方案(css) 文件上传input在各个浏览器里表现形式都不一样: ie6 ie7,8,9 ff chrome 这里介绍一种简单实用的,在各种浏览器下表现一致的美化 ...

  10. FeignCilent转发文件上传时遇到的一系列问题

    FeignCilent转发文件上传时遇到的一系列问题 这个问题在网上查到了一些资料,这里做下总结. 参考: https://www.jianshu.com/p/3ecbc0062411 https:/ ...

最新文章

  1. excel报表服务器作用,Excel各种图表的应用范围及用途介绍
  2. 这几款前端必备构建工具合辑,我们帮你整理好了!
  3. 修改正文中参考文献标注_论文写作中怎样正确插入参考文献,引用文献如何标注?...
  4. 【软件测试】软件调试基本技巧与策略
  5. Interceptor的用法以及和Filter的区别
  6. 提取某一个镇的行政边界_关于获取某个省份下面的 镇的 行政区划编码。
  7. 朝鲜欲对韩国发起大规模网络攻击 但计划被韩方挫败
  8. 第四周课程总结&实验报告。
  9. CentOS 操作系统初识
  10. 蓝桥杯特殊回文数C语言简易版
  11. 2019年安徽省学业水平考试计算机,2019年安徽省初中学业水平考试
  12. CSS3(新增样式)
  13. linux加减乘除的命令,加减乘除运算符
  14. 计算机网络手写笔记图片,一键收藏!清华学子这样记笔记!
  15. 深度学习课程大纲_MIT深度学习基础-2019视频课程分享
  16. QQ浏览器如何启用无痕模式
  17. 线性回归—求解介绍及回归扩展
  18. Angular2.0视频教程来了!
  19. 2022.12.9 英语背诵
  20. JavaScript常用库—jQuery

热门文章

  1. 进化计算-遗传算法之史上最全选择策略
  2. 【网易云课堂---轻松读书:番茄工作法】
  3. 【使用工具和软件汇总】
  4. 智能优化算法应用:基于麻雀搜索算法的TSP问题求解 - 附代码
  5. 从零基础入门Tensorflow2.0 ----六、32cifar10数据训练
  6. Solr服务器搭建与简单使用
  7. Android context.getSystemService的简单说明
  8. 30万奖金等你拿!Apache Flink 极客挑战赛入门指南(附Demo)
  9. bat 判断文件名字含有某些字段_PE文件头及其简单逆向运用
  10. mysql like 4种优化_mysql like优化_超级优化 清木桥