使用smartupload组件上传图片(jsp+servlet)
2013-08-08 16:31
627 查看
jsp部分:
<%@ page language="java" pageEncoding="GBK"%>
<%
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>
<title>smart_demo03.jsp</title>
</head>
<body>
<h3><font color="red">文件上传的操作---SmartUpload</font></h3>
<form action="<%=path %>/SmartUploadServlet" method="post" ENCTYPE="multipart/form-data">
图片:<input type="file" name="pic"/><br>
<input type="submit" value="上传"/>
</form>
${messages}
</body>
</html>
如果要进行文件的上传的操作的话,则必须对表单进行封装,也就说将表单中的ENCTYPE属性设置为: ”multipart/form-data”
servlet部分:
package com.smart;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;
import com.jspsmart.upload.File;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
/**
* 图片上传操作
*
* @version 2013/08/08 16:04:02
* @author
*/
public class SmartUploadServlet extends HttpServlet{
/**
* 允许上传的图片类型
*/
private static final String ALLOWEDFILESLIST = "jpg,gif,bmp";
/**
* 允许图片的大小
*/
private static final long MAXSIZE = 2 * 1024 * 1024;
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String messages = "";
String forward = "";
// 获取pageContext
PageContext pageContext = JspFactory.getDefaultFactory()
.getPageContext(this, request, response, null, true,
8192, true);
SmartUpload upload = new SmartUpload();
try {
// 初始化
upload.initialize(pageContext);
// 限制上传文件的大小
upload.setMaxFileSize(MAXSIZE);
// 设置允许上传的文件类型
upload.setAllowedFilesList(ALLOWEDFILESLIST);
// 准备上传
upload.upload();
// 系统时间做为文件名
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String sysDate = sdf.format(new Date());
// 保存文件
for (int i = 0; i < upload.getFiles().getCount(); i++) {
File file = upload.getFiles().getFile(i);
// 如果选择了文件
if (!file.isMissing()) {
// 获取图片的拓展名
String ext = upload.getFiles().getFile(i).getFileExt();
// 为文件重新命名
String fileName = sysDate + "." + ext;
file.saveAs("D:" + java.io.File.separator + "upload"
+ java.io.File.separator + fileName, file.SAVEAS_PHYSICAL);
}
}
} catch (java.lang.SecurityException e) {
messages = "上传的文件类型只允许为:" + ALLOWEDFILESLIST;
forward = "/index.jsp";
} catch (SmartUploadException e) {
messages = "上传文件失败!";
forward = "/index.jsp";
e.printStackTrace();
}
request.setAttribute("messages", messages);
request.getRequestDispatcher(forward).forward(request, response);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>SmartUploadServlet</servlet-name>
<servlet-class>com.smart.SmartUploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SmartUploadServlet</servlet-name>
<url-pattern>/SmartUploadServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
但是不要忘记了,一定要加上smartupload.jar 这个jar包
<%@ page language="java" pageEncoding="GBK"%>
<%
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>
<title>smart_demo03.jsp</title>
</head>
<body>
<h3><font color="red">文件上传的操作---SmartUpload</font></h3>
<form action="<%=path %>/SmartUploadServlet" method="post" ENCTYPE="multipart/form-data">
图片:<input type="file" name="pic"/><br>
<input type="submit" value="上传"/>
</form>
${messages}
</body>
</html>
如果要进行文件的上传的操作的话,则必须对表单进行封装,也就说将表单中的ENCTYPE属性设置为: ”multipart/form-data”
servlet部分:
package com.smart;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;
import com.jspsmart.upload.File;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
/**
* 图片上传操作
*
* @version 2013/08/08 16:04:02
* @author
*/
public class SmartUploadServlet extends HttpServlet{
/**
* 允许上传的图片类型
*/
private static final String ALLOWEDFILESLIST = "jpg,gif,bmp";
/**
* 允许图片的大小
*/
private static final long MAXSIZE = 2 * 1024 * 1024;
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String messages = "";
String forward = "";
// 获取pageContext
PageContext pageContext = JspFactory.getDefaultFactory()
.getPageContext(this, request, response, null, true,
8192, true);
SmartUpload upload = new SmartUpload();
try {
// 初始化
upload.initialize(pageContext);
// 限制上传文件的大小
upload.setMaxFileSize(MAXSIZE);
// 设置允许上传的文件类型
upload.setAllowedFilesList(ALLOWEDFILESLIST);
// 准备上传
upload.upload();
// 系统时间做为文件名
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String sysDate = sdf.format(new Date());
// 保存文件
for (int i = 0; i < upload.getFiles().getCount(); i++) {
File file = upload.getFiles().getFile(i);
// 如果选择了文件
if (!file.isMissing()) {
// 获取图片的拓展名
String ext = upload.getFiles().getFile(i).getFileExt();
// 为文件重新命名
String fileName = sysDate + "." + ext;
file.saveAs("D:" + java.io.File.separator + "upload"
+ java.io.File.separator + fileName, file.SAVEAS_PHYSICAL);
}
}
} catch (java.lang.SecurityException e) {
messages = "上传的文件类型只允许为:" + ALLOWEDFILESLIST;
forward = "/index.jsp";
} catch (SmartUploadException e) {
messages = "上传文件失败!";
forward = "/index.jsp";
e.printStackTrace();
}
request.setAttribute("messages", messages);
request.getRequestDispatcher(forward).forward(request, response);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>SmartUploadServlet</servlet-name>
<servlet-class>com.smart.SmartUploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SmartUploadServlet</servlet-name>
<url-pattern>/SmartUploadServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
但是不要忘记了,一定要加上smartupload.jar 这个jar包
相关文章推荐
- 使用jspsmartupload组件在servlet里面上传文件
- JSP+Servlet中使用jspsmartupload.jar进行图片上传下载
- 使用jspSmartUpload组件实现图片的上传
- 使用JspSmartUpload组件上传文件
- 项目开发技巧(四):使用JspSmartupload实现文件上传下载(一):JspSmart之upload组件源码及使用
- 使用smartupload进行图片上传(servlet版,springmvc版)
- 在jsp中使用smartupload组件上传文件ggg
- 在jsp中使用smartupload组件上传文件
- Servlet.service() for servlet jsp threw exception java.lang.Il在jsp中使用jspsmartupload组件下载文件时将抛出异常的解决方案
- 使用smartupload组件实现jsp+jdbc上传下载文件实例解析
- 搞定使用jspsmartupload 组件进行上传和下载出现的乱码问题。
- 在jsp中使用smartupload组件上传文件
- 在jsp中使用smartupload组件上传文件
- jspSmartUpload使用详解(JSP上传下载组件)
- JSP+Servlet使用commons.fileupload和commons.io完成图片上传
- 使用jspSmartUpload控件上传图片
- 【转】在jsp中使用smartupload组件上传文件
- 在jsp中使用smartupload组件上传文件- -
- 在jsp中使用smartupload组件上传文件
- JSP使用SmartUpload上传图片