您的位置:首页 > Web前端 > JavaScript

【转】JSP使用上传文件,并生产高清缩略图示例

2016-01-04 22:06 726 查看
原文地址:http://blog.csdn.net/yakson/article/details/9875731

前言

刚开始本来只想来测试一下Thumbnails生成缩略图的效果的,顺便来学一下jsp文件,开始没有使用commons-fileupload上 传组件,自己用纯jsp代码来编写,过程相当曲折。所以就不建议大家去编写纯JSP的上传代码了,想写的可以参考下commons-fileupload 的源码,里面很详细。

一、JSP上传文件

大家都知道,上传文件是以二进制上传的,这样可以让文件上传,所以JSP要做到将文件以二进制上传,我们再HTML的表单提交时就要设置enctype="multipart/form-data",这个大家应该都很清楚了。

首先我先将jar包引用列出来,大家先找好这几个jar文件,引入项目

commons-fileupload-1.2.2.jar

commons-io-2.0.1.jar

thumbnailator-0.4.2.jar

先上一下上传页面的JSP代码,其实很简单,放一个file文件选择框就可以,我为了测试,顺便加了一个文本框。

index.jsp

[html] view plaincopyprint?

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>缩略图生成示例</title>

</head>

<body>

<h1>上传图片</h1>

<form name="uploadForm" action="upload.jsp" method="post"

enctype="multipart/form-data">

<input type="text" name="name" />

<input type="file" name="imgPath" />

<input type="submit" value="提交" />

</form>

</body>

</html>

二、编写上传文件代码

upload.jsp

[html] view plaincopyprint?

<%@page import="net.coobird.thumbnailator.Thumbnails"%>

<%@page import="org.apache.commons.fileupload.FileItem"%>

<%@page import="java.util.Iterator"%>

<%@page import="java.util.Hashtable"%>

<%@page import="java.util.Map"%>

<%@page import="org.apache.commons.fileupload.FileUploadException"%>

<%@page import="java.util.List"%>

<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>

<%@page import="org.apache.commons.fileupload.FileItemFactory"%>

<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>

<%@page import="java.io.File"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body>

<%

request.setCharacterEncoding("UTF-8");

String name = "";

String imgPath = "";

String filePath = "";

if (ServletFileUpload.isMultipartContent(request)) {

try {

FileItemFactory factory = new DiskFileItemFactory();

ServletFileUpload upload = new ServletFileUpload(factory);

upload.setHeaderEncoding("UTF-8");

upload.setFileSizeMax(1024000L);//单个上传文件最大值

upload.setSizeMax(2048000L);//整个请求的大小最大值

List list = upload.parseRequest(request);

Map _fields = new Hashtable();

Iterator it = list.iterator();

String tempPath = request.getRealPath("/temp/");

String UUID = java.util.UUID.randomUUID().toString();

while (it.hasNext()) {

FileItem item = (FileItem) it.next();

if (!item.isFormField()) {

String fileImg = item.getName();

//获取图片后缀

String suffix = fileImg.substring(fileImg.lastIndexOf(".")

, fileImg.length());

filePath = tempPath + File.separator + UUID + suffix;

// 建立目标文件

File file = new File(filePath);

//将文件写入到临时文件上传目录

item.write(file);

_fields.put(item.getFieldName(), UUID + suffix);

} else {

_fields.put(item.getFieldName(), item.getString());

}

}

name = _fields.get("name").toString();

imgPath = _fields.get("imgPath").toString();

String imgPath_s = imgPath.substring(0, imgPath.lastIndexOf("."));

String imgPath_s_suffix = imgPath.substring(imgPath.lastIndexOf(".")

, imgPath.length());

//生成缩略图

String filePath_s_w = tempPath + File.separator + imgPath_s

+ "_w" + imgPath_s_suffix;

String filePath_s_h = tempPath + File.separator + imgPath_s

+ "_h" + imgPath_s_suffix;

String filePath_s_m = tempPath + File.separator + imgPath_s

+ "_m" + imgPath_s_suffix;

//宽为准

Thumbnails.of(filePath)

.size(300, 200)

.toFile(filePath_s_w);

//中图

Thumbnails.of(filePath)

.size(500, 400)

.toFile(filePath_s_m);

//高为准

Thumbnails.of(filePath)

.size(200, 300)

.toFile(filePath_s_h);

} catch (Exception e) {

out.write(e.getMessage());

e.printStackTrace();

}

} else {

name = request.getParameter("name");

imgPath = request.getParameter("imgPath");

}

%>

name:<%=name%><br />

imgPath<%=imgPath%><br />

</body>

</html>

我就代码简单说明一下,我们用ServletFileUpload.isMultipartContent(request)来判断用户的表单是否是以二
进制上传,从而改变获取提交表单数据的模式,因为从二进制里提交的表单,你从request.getParameter中是获取不到值的。

通过commons-fileupload组建,我们很容易的获取到了用户表单上传文件流,并保持到了我们服务器磁盘中。

此示例组要是测试图片生成缩略图,所以就没考虑上传的文件类型,我就当上传的图片类型了,如果上传其他类型的文件,页面会异常,但是文件可以上传的。

好我们来看下生成缩略图的代码,仅仅简单的一行代码。

[java] view plaincopyprint?

//size(宽度, 高度)

/*

* 若图片横比200小,高比300小,不变

* 若图片横比200小,高比300大,高缩小到300,图片比例不变

* 若图片横比200大,高比300小,横缩小到200,图片比例不变

* 若图片横比200大,高比300大,图片按比例缩小,横为200或高为300

*/

Thumbnails.of("c:/images/1280x1024.jpg")

.size(200, 300)

.toFile("c:/200x300.jpg");

相信代码意思已经不用解释了,关于Thumbnails的其他用法,大家还是去官方网站上查看吧,下载的示例和文档中有详细的说明。

在谷歌开源项目中开源找到地址:
http://code.google.com/p/thumbnailator/downloads/list
如果需要我做的示例的项目源码,留言时写下你的邮箱,我看到会把源码发到你的邮箱。

最后附上项目结构截图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: