SmartUpload实现文件上传时file和表单文本同时提交的问题
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和表单文本同时提交的问题的更多相关文章
- [原创]java WEB学习笔记49:文件上传基础,基于表单的文件上传,使用fileuoload 组件
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- js文件上传原理(form表单 ,FormData + XHR2 + FileReader + canvas)
目录 form表单上传 FormData + XHR2 + FileReader + canvas 无刷新本地预览压缩上传实例 目前实现上传的方式 浏览器小于等于IE9(低版本浏览器)使用下面的方式实 ...
- Spring MVC 文件上传、Restful、表单校验框架
目录 文件上传 Restful Restful 简介 Rest 行为常用约定方式 Restful开发入门 表单校验框架 表单校验框架介绍 快速入门 多规则校验 嵌套校验 分组校验 综合案例 实用校验范 ...
- jqm文件上传,上传图片,jqm的表单操作,jqm的ajax的使用,jqm文件操作大全,文件操作demo
近期在论坛中看到.在使用html5中上传图片或文件,出现各种问题. 这一方面,我也一直没有做过,今天就抽出了一点时间来学习一下.如今的演示样例已经ok了,我就给大家分享一下,希望对大家有帮助. 好吧. ...
- 八 SpringMVC文件上传,必须设置表单提交为post
1 修改Tomcat配置,本地目录映射 那么在server.xml中体现为: 测试一下是否设置成功: 2 引入jia包 3 配置多媒体解析器 3 jsp开启图片上传 4 Controller层设置 ...
- 基于Metronic的Bootstrap开发框架经验总结(5)--Bootstrap文件上传插件File Input的使用
Bootstrap文件上传插件File Input是一个不错的文件上传控件,但是搜索使用到的案例不多,使用的时候,也是一步一个脚印一样摸着石头过河,这个控件在界面呈现上,叫我之前使用过的Uploadi ...
- Bootstrap文件上传插件File Input的使用
基于Metronic的Bootstrap开发框架经验总结(5)--Bootstrap文件上传插件File Input的使用 Bootstrap文件上传插件File Input是一个不错的文件上传控件, ...
- 文件上传时出现 Processing of multipart/form-data request failed. Unexpected EOF read on the socket错误
上传时一直出现这个错误,修改tomcat的server.xml文件,更改tomcat版本,也查阅了网上的很多解决办法,都不能解决问题. 后在stackoverflow的一篇文章上找到了解决方法: 加上 ...
- (转)基于Metronic的Bootstrap开发框架经验总结(5)--Bootstrap文件上传插件File Input的使用
http://www.cnblogs.com/wuhuacong/p/4774396.html Bootstrap文件上传插件File Input是一个不错的文件上传控件,但是搜索使用到的案例不多,使 ...
随机推荐
- mysql5.8安装指南
一.安装mysql yum源 从官网http://dev.mysql.com/downloads/repo/yum/下载mysql最新的yum源的rpm安装包 wget http://repo.mys ...
- 在Asp.net 4.0 中动态注册HttpModule
using System; using System.Web; using Microsoft.Web.Infrastructure; namespace MvcApplication1 { publ ...
- linux一些目录功能
(1)/etc ----存放配置文件和子目录 (2)/dev -----是linux的外设文件,dev是devxe(设备)的缩写,在这个目录下存放的是所有linux的外设文件,.在li ...
- bat调用bat的一个巨坑
[一个巨坑] a.bat的内容:echo 1b.batecho 2执行结果:运行a.bat时,输出1,然后调用b.bat, 但是 echo 2 显示不出来. bat怎么调用bat文件并返回? 例如主文 ...
- scrollview背景头部拉伸
a - (void)viewDidLoad { [super viewDidLoad]; self.tableView.contentInset = UIEdgeInsetsMake(kImageOr ...
- ZOJ 3209 Treasure Map (Dancing Links)
Treasure Map Time Limit: 2 Seconds Memory Limit: 32768 KB Your boss once had got many copies of ...
- java file类的常用方法和属性
1 常用方法 a.createNewFile方法 public boolean createNewFile() throws IOException 该方法的作用是创建指定的文件.该方法只 ...
- IE6/IE7/IE8兼容H5标签
可以使用html5shiv(html5shiv主要解决HTML5提出的新元素不被IE6-8识别,这些新元素不能作为父节点包裹子元素,并且不能应用CSS样式)来解决 <!--[if lt IE 9 ...
- windows7内核驱动开发试验环境配置
首先配置环境参照这个: http://blog.csdn.net/qing666888/article/details/50858272 然后在win10里可能由于没有做测试签名因此一直没有成功加载驱 ...
- .NET MVC TempData、ViewData、ViewBag
说明: 原文作者贤新 原文地址:http://www.cnblogs.com/chenxinblogs/p/4852813.html ViewData和ViewBag主要用于将数据从控制器中传递到视图 ...