[color=blue]selectImg.jsp页面[/color]
<%@ page language="java" import="java.util.* " pageEncoding="GBK"%>
<html>
<head>
<title>上传图片</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<base target="_self" />
<body>
<div>
<form action="doUploadImage.jsp" method="post">
本地选择:
<input type="file" name="selPicture" id="selPicture"
style="width: 330px; height: 23px; font-size: 16px">
<input type="submit" name="upload" id="upload" value="上传"
style="width: 70px; height: 25px">
</form>
</div>
</body>
</html>
[color=blue]2、doUploadImage.jsp页面[/color]
<%@ page language="java"
import="java.util.*,com.jspsmart.upload.*,java.io.*"
pageEncoding="GBK"%>
<%
request.setCharacterEncoding("GBK");
long size = 5 * 1024 * 1024;//允许上传最大值为5MB
String fileType = "jpg,gif";//允许上传文件类型
String imgName = null;//图片名称
byte[] data = null;//数据
String filePath = "";//文件路径
//得到服务器目录webroot下的ImageFiles目录的完整路径
String path = super.getServletContext().getRealPath("/ImageFiles");
SmartUpload su = new SmartUpload();
//初始化
su.initialize(pageContext);
su.setMaxFileSize(size);
su.setAllowedFilesList(fileType);
//上载文件
su.upload();
//循环取得所有上载的文件
Files files = su.getFiles();
if (files != null) {
//如果文件路径不存在则生成路径
java.io.File fileDir = new java.io.File(path);
if (!fileDir.exists()) {
fileDir.mkdirs();
}
//取出文件
for (int i = 0; i < files.getCount(); i++) {
File file = files.getFile(i);
if (file.isMissing())
continue;
if ("selPicture".equals(file.getFieldName())) {
String type = file.getFilePathName();
type = type.substring(type.lastIndexOf("."));
imgName = UUID.randomUUID().toString();//生成uuid作为图片的名称
imgName += type;
filePath = path + "/" + imgName;
//保存到指定文件
file.saveAs(filePath);
//读取文件
data = readFile(filePath);
break;
}
}
}
if (data == null) {
out.print("没有图片");
} else {
out.print("图片上传成功");
}
%>
<%!byte[] readFile(String filePath) {
ByteArrayOutputStream bos = null;
try {
FileInputStream fs = new FileInputStream(filePath);
bos = new ByteArrayOutputStream(5 * 1024 * 1024);
byte[] b = new byte[1024];
int len;
while ((len = fs.read(b)) != -1) {
bos.write(b, 0, len);
}
fs.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (bos == null) {
return null;
} else {
return bos.toByteArray();
}
}
%>
<%@ page language="java" import="java.util.* " pageEncoding="GBK"%>
<html>
<head>
<title>上传图片</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<base target="_self" />
<body>
<div>
<form action="doUploadImage.jsp" method="post">
本地选择:
<input type="file" name="selPicture" id="selPicture"
style="width: 330px; height: 23px; font-size: 16px">
<input type="submit" name="upload" id="upload" value="上传"
style="width: 70px; height: 25px">
</form>
</div>
</body>
</html>
[color=blue]2、doUploadImage.jsp页面[/color]
<%@ page language="java"
import="java.util.*,com.jspsmart.upload.*,java.io.*"
pageEncoding="GBK"%>
<%
request.setCharacterEncoding("GBK");
long size = 5 * 1024 * 1024;//允许上传最大值为5MB
String fileType = "jpg,gif";//允许上传文件类型
String imgName = null;//图片名称
byte[] data = null;//数据
String filePath = "";//文件路径
//得到服务器目录webroot下的ImageFiles目录的完整路径
String path = super.getServletContext().getRealPath("/ImageFiles");
SmartUpload su = new SmartUpload();
//初始化
su.initialize(pageContext);
su.setMaxFileSize(size);
su.setAllowedFilesList(fileType);
//上载文件
su.upload();
//循环取得所有上载的文件
Files files = su.getFiles();
if (files != null) {
//如果文件路径不存在则生成路径
java.io.File fileDir = new java.io.File(path);
if (!fileDir.exists()) {
fileDir.mkdirs();
}
//取出文件
for (int i = 0; i < files.getCount(); i++) {
File file = files.getFile(i);
if (file.isMissing())
continue;
if ("selPicture".equals(file.getFieldName())) {
String type = file.getFilePathName();
type = type.substring(type.lastIndexOf("."));
imgName = UUID.randomUUID().toString();//生成uuid作为图片的名称
imgName += type;
filePath = path + "/" + imgName;
//保存到指定文件
file.saveAs(filePath);
//读取文件
data = readFile(filePath);
break;
}
}
}
if (data == null) {
out.print("没有图片");
} else {
out.print("图片上传成功");
}
%>
<%!byte[] readFile(String filePath) {
ByteArrayOutputStream bos = null;
try {
FileInputStream fs = new FileInputStream(filePath);
bos = new ByteArrayOutputStream(5 * 1024 * 1024);
byte[] b = new byte[1024];
int len;
while ((len = fs.read(b)) != -1) {
bos.write(b, 0, len);
}
fs.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (bos == null) {
return null;
} else {
return bos.toByteArray();
}
}
%>