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 here
this.config = config;
} }

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

  1. [原创]java WEB学习笔记49:文件上传基础,基于表单的文件上传,使用fileuoload 组件

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  2. js文件上传原理(form表单 ,FormData + XHR2 + FileReader + canvas)

    目录 form表单上传 FormData + XHR2 + FileReader + canvas 无刷新本地预览压缩上传实例 目前实现上传的方式 浏览器小于等于IE9(低版本浏览器)使用下面的方式实 ...

  3. Spring MVC 文件上传、Restful、表单校验框架

    目录 文件上传 Restful Restful 简介 Rest 行为常用约定方式 Restful开发入门 表单校验框架 表单校验框架介绍 快速入门 多规则校验 嵌套校验 分组校验 综合案例 实用校验范 ...

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

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

  5. 八 SpringMVC文件上传,必须设置表单提交为post

    1 修改Tomcat配置,本地目录映射 那么在server.xml中体现为: 测试一下是否设置成功: 2 引入jia包   3 配置多媒体解析器 3 jsp开启图片上传 4 Controller层设置 ...

  6. 基于Metronic的Bootstrap开发框架经验总结(5)--Bootstrap文件上传插件File Input的使用

    Bootstrap文件上传插件File Input是一个不错的文件上传控件,但是搜索使用到的案例不多,使用的时候,也是一步一个脚印一样摸着石头过河,这个控件在界面呈现上,叫我之前使用过的Uploadi ...

  7. Bootstrap文件上传插件File Input的使用

    基于Metronic的Bootstrap开发框架经验总结(5)--Bootstrap文件上传插件File Input的使用 Bootstrap文件上传插件File Input是一个不错的文件上传控件, ...

  8. 文件上传时出现 Processing of multipart/form-data request failed. Unexpected EOF read on the socket错误

    上传时一直出现这个错误,修改tomcat的server.xml文件,更改tomcat版本,也查阅了网上的很多解决办法,都不能解决问题. 后在stackoverflow的一篇文章上找到了解决方法: 加上 ...

  9. (转)基于Metronic的Bootstrap开发框架经验总结(5)--Bootstrap文件上传插件File Input的使用

    http://www.cnblogs.com/wuhuacong/p/4774396.html Bootstrap文件上传插件File Input是一个不错的文件上传控件,但是搜索使用到的案例不多,使 ...

随机推荐

  1. Jenkins+Jmeter+Ant接口持续集成

    修改时间 修改内容 修改人 2016.5.22 创建 刘永志 2016.6.15 完成 刘永志 前言: 为什么要用Jmeter做接口测试: 当选择这套方案的时候,很多人会问,为什么选择Jmeter做C ...

  2. 基于log4net的支持动态文件名、按日期和大小自动分割文件的日志组件

    最近处理一个日志功能,用log4net的配置不能完全满足要求,所以在其基础上简单封装了一下,支持以下功能: 1 零配置 内置默认配置,引用dll后不需要添加或修改任何配置文件也可以使用 2 动态指定文 ...

  3. UE3 ExampleGame Android版无法运行解决方案

    首先将 UE3\UnrealEngine3\UDKGame\Build\Android 文件夹 拷贝到 ExampleGame\Build下面.里面有相应的android 配置文件.xml 若果此时 ...

  4. 02.Sencha ExtJS 6 - What is Viewport?

    什么是Viewport? Viewport (Ext.container.Viewport)是一个专门的容器用于可视应用领域(浏览器窗口).Viewport渲染自身到网页的documet body区域 ...

  5. 今天使用bootstrap中的tagsinput控件,碰到个小问题

    我直接使用时,后台一直报Uncaught Can't add objects when itemValue option is not set错误, 代码如下: <div> <inp ...

  6. strcpy 和 strcat

    strcpy 原型:char *strcpy( char *dest, char *src )  头文件:#include <string.h> 功能:将src地址开始且含有NULL结束符 ...

  7. 循序渐进Python3(八) -- 1 -- socket进阶

    IO多路复用 I/O多路复用指:通过一种机制,可以监视多个描述符,一旦某个描述符就绪(一般是读就绪或者写就绪),能够通知程序进行相应的读写操作. Linux中的 select,poll,epoll 都 ...

  8. android Activity绑定Service

    activity可以绑定Service,并且可以调用Service中定义的方法 Service代码:在里面多了一个IBinder;个人理解是用来与Activity绑定的主要通道: public cla ...

  9. 通过pinyin4j.jar将(汉字拼音混合字符串)转化成字母首字母

    通过pinyin4j.jar将(汉字拼音混合字符串)转化成字母首字母 例如 我的中国心    ==> wdzgx 我的中国心ya ==> wdzgxya woai我的中国 ==> w ...

  10. Android笔记:如何在Fragment里使用findViewById()方法?

    在Activity中,可以直接用findViewById(int id),通过xml的id来找到对应的View. 查找官方的api,具体如下: https://developer.android.go ...