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是一个不错的文件上传控件,但是搜索使用到的案例不多,使 ...
随机推荐
- LIKE模糊查询的通配符
LIKE模糊查询的通配符 通配符 说明 示例 % 包含零个或多个字符的任意字符串. WHERE title LIKE '%computer%' 将查找在书名中任意位置包含单词 "comput ...
- Net分布式系统之四:RabbitMQ消息队列应用
消息通信组件Net分布式系统的核心中间件之一,应用与系统高并发,各个组件之间解耦的依赖的场景.本框架采用消息队列中间件主要应用于两方面:一是解决部分高并发的业务处理:二是通过消息队列传输系统日志.目前 ...
- BZOJ1801:[Ahoi2009]chess 中国象棋
Time Limit: 10 Sec Memory Limit: 64 MB Description 在N行M列的棋盘上,放若干个炮可以是0个,使得没有任何一个炮可以攻击另一个炮. 请问有多少种放置 ...
- 三表联查,这是我目前写过的最长的sql语句,嗯嗯,果然遇到问题才能让我更快成长,更复杂的语句也有了一些心得了
select sum(amount),sum(card_number) from sy_user inner join sy_admin on sy_user.customer_id=sy_admin ...
- mave的变量说明
Maven提供了三个隐式的变量可以用来访问环境变量,POM信息,和Maven Settings env env变量,暴露了你操作系统或者shell的环境变量.便 如在Maven POM中一个对${en ...
- VMware中linux硬盘空间不足的解决方法
相信很多人都和我一样是利用虚拟机安装linux的,在玩转linux的时候,可能就会遇到系统提示磁盘空间不足的情况.由于VMware中当初装系统时的设置的最大磁盘容量是不可以动态修改的,所以为我们使用带 ...
- SeekBar 圆角问题
用图片做背景色,最后处理成.9.png的.用普通png图片做背景,则两边会有圆角出现,原因是图片不适合SeekBar尺寸,因而被拉伸或压缩,从而产生圆角. <?xml version=" ...
- 触摸屏测试:Tslib
触摸屏测试:Tslib(配置了一天,心累) 总结:网上有许多关于tslib的步骤/错误总结,所以这里只是提几个我觉得比较棘手的问题: 1,我明明给arm-linux-gcc配置了环境变量,为什么还是提 ...
- linux中用户、组的管理(密码管理、权限管理及其修改用户、组)(转)
process(进程) 1 计算资源 权限 用户(获取资源或服务的凭证或标识) 用户,容器,关联权限:用户组(标识符),方便地指派权限 2 用户.组.权限 安全上下文(secure context ...
- java中的 FileWriter类 和 FileReader类的一些基本用法
1,FileWriter类(字符输出流类) |--用来写入字符文件的便捷类.此类的构造方法假定默认字符编码和默认字节缓冲区大小都是可接受的.要自己指定这些值,可以先在 FileOutputStream ...