jsp实现照片的上传和下载
一.进入页面
@RequestMapping("/upimage")
public String upImageDemo() {
return "/testImageUp";
}
二.上传照片
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
System.out.println("path:"+path);
System.out.println("basePath:"+basePath);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>多图片上传</title>
<script src="${basePath}../js/jquery-1.11.0.min.js"></script>
<script src="${basePath}../js/upImage.js"></script>
<body>
<form enctype="multipart/form-data"
action="${basePath}upimage2" method="post">
<div id="preview"></div>
<div id="file-input">
<input id="id0" type="file" name="/upload/"
οnchange="previewImage(this)">
</div>
<input type="submit" value="提交">
</form>
</body>
</html>
三.进入跳转的页面进行操作(此处因为只能页面传页面,而页面有在WEB-INF下面,只能通过这
个方式跳转到指定页面)
@RequestMapping("/upimage2")
public void image2Servicec(HttpServletRequest request,HttpServletResponse response){
try {
request.getRequestDispatcher("/WEB-INF/views/testImageUp2.jsp").forward(request,response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
四.进行下载到服务器的指定目录
<%@ page language="java" import="java.util.*,java.io.*"
pageEncoding="utf-8"%>
<%@ page import="com.jspsmart.upload.SmartUpload"%>
<%@ page import="javax.servlet.jsp.tagext.TryCatchFinally"%>
<%@ page import="javax.imageio.ImageIO"%>
<%@ page import="java.awt.image.BufferedImage"%>
<%@ page import="com.sun.image.codec.jpeg.JPEGImageEncoder"%>
<%@ page import="com.sun.image.codec.jpeg.JPEGCodec"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%
System.out.println("==========into testImageUp2.jsp===========");
//这个处理文件实现了对图片的压缩
SmartUpload mySmartUpload = new SmartUpload();
long file_size_max = 40000000;
String fileName2;//文件名
String ext;//文件扩展名
String testVar;
String url = "/upload/";//应保证在根目录中有此目录的存在
System.out.println("url:"+url);
//初始化
System.out.println("pageContext:"+pageContext);
mySmartUpload.initialize(pageContext);
//只允许上载此类文件
try {
//支持上载文件的后缀名
mySmartUpload.setAllowedFilesList("jpg,gif");
//mySmartUpload.setAllowedFilesList("jpg,gif,jpeg,png");
//不支持制定的后缀
mySmartUpload.setDeniedFilesList("exe");
//上载文件
mySmartUpload.upload();//不指定编码的upload()方法
// mySmartUpload.upload("utf-8");//指定编码的upload()方法
} catch (Exception e) {
out.print("<script type=\"text/javascript\">");
out.print("window.alert(\"只允许上传.jpg和.gif类型图片文件\");");
out.print("window.location=\"upload.html;\"");
out.print("</script>");
}
try {
System.out.println("the files size:"+(mySmartUpload.getFiles().getCount()-1));
for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) {
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
if(myFile.getFileExt() == "" || myFile.getFileExt() == null){
continue;
}
/* if (myFile.isMissing()) {
out.print("<script type=\"text/javascript\">");
out.print("window.alert(\"请先选择要上传的文件\");");
out.print("window.location=\"upload.html;\"");
out.print("</script>");
} else { */
String myFileName = myFile.getFileName();//取得上载的文件的文件名
ext = myFile.getFileExt();//取得后缀名
if (!(ext.length() > 0)) {
//out.println("**************myFileName的名称是:" + myFileName);
}
int file_size = myFile.getSize();//取得文件的大小
String saveUrl = "";//文件保存路径
if (file_size < file_size_max) {
//更改文件名,取得当前上传时间的毫秒数值
Calendar calendar = Calendar.getInstance();
String fileName = String.valueOf(calendar.getTimeInMillis());//设置新的文件名
saveUrl += fileName + "." + ext;
myFile.saveAs(saveUrl, mySmartUpload.SAVE_PHYSICAL);
//上传完成,开始生成缩略图
java.io.File file = new java.io.File(saveUrl);//读入刚才上传的文件
out.println("ext=" + ext);
String newUrl = request.getRealPath("/") + url + fileName + "_min." + ext;//新的缩略图保存地址
java.awt.Image src = javax.imageio.ImageIO.read(file);//构造Image对象
float tagSize = 200;
int old_w = src.getWidth(null);//得到原图宽
int old_h = src.getHeight(null);//得到原图高
int new_w = 0;
int new_h = 0;
int tempSize;//设置临时大小
float tempDouble;
if (old_w > old_h) {
tempDouble = old_w / tagSize;
} else {
tempDouble = old_h / tagSize;
}
new_w = Math.round(old_w / tempDouble);
new_h = Math.round(old_h / tempDouble);
BufferedImage tag = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0, new_w, new_h, null);//绘制缩小后的图
FileOutputStream newImage = new FileOutputStream(newUrl);//输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newImage);
encoder.encode(tag);//近JPEG编码
newImage.close();
} else {
out.print("<script type=\"text/javascript\">");
out.print("window.alert(\"上传文件大小不能超过\"+(file_size_max/1000)+\"K\");");
out.print("window.location=\"upload.html;\"");
out.print("</script>");
}
/* } */
}
} catch (Exception e) {
e.printStackTrace();
}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>处理上传图片的JSP</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">
</head>
<body>
</body>
</html>