前言
刚开始本来只想来测试一下thumbnails生成缩略图的效果的,顺便来学一下文件,开始没有使用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
<%@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>
<%@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组建,我们很容易的获取到了用户表单上传文件流,并保持到了我们服务器磁盘中。
此示例组要是测试图片生成缩略图,所以就没考虑上传的文件类型,我就当上传的图片类型了,如果上传其他类型的文件,页面会异常,但是文件可以上传的。
好我们来看下生成缩略图的代码,仅仅简单的一行代码。
//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");
段子就是本黄书