image_upload_main.jsp:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>图片上传主界面</TITLE>
</head>
<body bgcolor="#efefef">
<div align="left"><br><p>
<form name="baseinfo" action="#" method="post" enctype="multipart/form-data">
图片类别: <select name="file_type">
<%
if(imagetypes!=null&&imagetypes.size()>0){
for(int m=0;m<imagetypes.size();m++){
String typeName=((Imagetype)imagetypes.getAt(m)).getType_name();
out.print("<option value="+typeName+">");
out.print(typeName);
out.print("</option>");
}
}else{
out.print("<option>没有为图片分类</option>");
}
%>
</select> <a href="imagetype_addedit.jsp?opersType=add">添加类型</a><p>
图片说明: <br><textarea rows="7" name="remark" cols="60"></textarea><p>
<%
Float imageSize=Float.valueOf(ConfigFile.UPLOADIMAGESIZE);
Float cImageSize=imageSize/(1024*1024);
%>
请选择上传文件(小于<%=cImageSize%>M)<br>
<input type="file" name="file"/><p>
<input type="button" value="返回" onclick="javascript:history.go(-1);">
<input type="submit" name="Submit" value="上传" onclick="checkAndSubmit();"/>
</form>
</div>
<script type="text/javascript">
function checkAndSubmit(){
var remark=document.getElementById("remark").value;
var file_type=document.getElementById("file_type").value;
var fileValue=document.getElementById("file").value;
if(file_type==null||file_type==""){
alert("没有为图片分类!");
return;
}
if(remark==null||remark==""){
alert("请添加图片说明!");
return;
}
if(fileValue==null||fileValue==""){
alert("请选择要上传的图片!");
return;
}
var index=fileValue.lastIndexOf(".");
var fm=fileValue.substring(index+1);
if(fm=="jpg"||fm=="gif"||fm=="JPG"||fm=="GIF"){
var index=fileValue.lastIndexOf("\\");
var fileName=fileValue.substring(index+1);
document.baseinfo.action="image_upload.jsp?remark="+remark+"&fileName="+fileName+"&file_type="+file_type+"&fileValue="+fileValue+"";
document.baseinfo.submit();
}else{
alert("不支持该格式, 请正确选择!");
return;
}
}
</script>
</body>
</html>
image_upload.jsp:
<%
String platform=request.getParameter("platform");
String remark=request.getParameter("remark");
String cremark=new String(remark.getBytes("iso-8859-1"),"gbk");
String file_type=request.getParameter("file_type");
String cfile_type=new String(file_type.getBytes("iso-8859-1"),"gbk");
String fileName=request.getParameter("fileName");
String cfileName=new String(fileName.getBytes("iso-8859-1"),"gbk");
String fileValue=request.getParameter("fileValue");
String cfileValue=(new String(fileValue.getBytes("iso-8859-1"),"gbk")).replace("\\\\","\\");
String file_name="";
String file_size="";
String file_ext="";
String file_path="";
String new_name="";
String cstrname="";
java.io.File fileDir=new java.io.File(ConfigFile.IMAGEPATH);
String[] files=fileDir.list();
//判断是否为文件
java.io.File file1=new java.io.File(cfileValue);
if(!file1.exists()){
%>
<script type="text/javascript">
alert("目录下不存在该文件!");
window.location.href="image_upload_main.jsp";
</script>
<%
}else{
//判断是否已上传
for(int n=0;n<files.length;n++){
String checkFile=files[n];
if(checkFile.equals(cfileName)){
%>
<script type="text/javascript">
alert("不能重复上传!");
window.location.href="image_upload_main.jsp";
</script>
<%
}
}
}
try{
SmartUpload su=new SmartUpload();
su.initialize(pageContext);
su.setMaxFileSize(ConfigFile.UPLOADIMAGESIZE);
su.upload();
//上传文件的存放路径
file_path=ConfigFile.IMAGEPATH.replace("//","/");
int count=su.save(file_path);
com.jspsmart.upload.File file=su.getFiles().getFile(0);
file_name=file.getFileName();
file_size=String.valueOf(file.getSize());
file_ext=file.getFileExt();
int name_index=file_name.lastIndexOf(".");
new_name=file_name.substring(0,name_index);
if(file_size.equals("0")){
java.io.File file2=new java.io.File(ConfigFile.IMAGEPATH+file_name);
file2.delete();
}
//-----------------------上传完成,开始生成缩略图-------------------------
java.io.File newfile = new java.io.File(ConfigFile.IMAGEPATH+file_name); //读入刚才上传的文件
Calendar calendar=Calendar.getInstance();
Long tim=calendar.getTimeInMillis();
cstrname=String.valueOf(tim);
String newurl_min=ConfigFile.IMAGEPATH+cstrname+"_min."+file_ext; //新的缩略图保存地址
String newurl_mid=ConfigFile.IMAGEPATH+cstrname+"_mid."+file_ext; //新的缩略图保存地址
String newurl_big=ConfigFile.IMAGEPATH+cstrname+"_big."+file_ext; //新的缩略图保存地址
java.awt.Image src = javax.imageio.ImageIO.read(newfile); //构造Image对象
float tagsize_min=ConfigFile.IMAGE_MIN_WIDTH; //小图
float tagsize_mid=ConfigFile.IMAGE_MID_WIDTH; //中图
float tagsize_big=ConfigFile.IMAGE_BIG_WIDTH; //大图
int old_w=src.getWidth(null); //得到源图宽
int old_h=src.getHeight(null); //得到源图高
int new_w_min=0;
int new_h_min=0;
int new_w_mid=0;
int new_h_mid=0;
int new_w_big=0;
int new_h_big=0;
float tempdouble_min;
float tempdouble_mid;
float tempdouble_big;
if(old_w>old_h){
tempdouble_min=old_w/tagsize_min;
tempdouble_mid=old_w/tagsize_mid;
tempdouble_big=old_w/tagsize_big;
}else{
tempdouble_min=old_h/tagsize_min;
tempdouble_mid=old_h/tagsize_mid;
tempdouble_big=old_h/tagsize_big;
}
new_w_min=Math.round(old_w/tempdouble_min);
new_h_min=Math.round(old_h/tempdouble_min);
new_w_mid=Math.round(old_w/tempdouble_mid);
new_h_mid=Math.round(old_h/tempdouble_mid);
new_w_big=Math.round(old_w/tempdouble_big);
new_h_big=Math.round(old_h/tempdouble_big);
BufferedImage tag_min = new BufferedImage(new_w_min,new_h_min,BufferedImage.TYPE_INT_RGB);
BufferedImage tag_mid = new BufferedImage(new_w_mid,new_h_mid,BufferedImage.TYPE_INT_RGB);
BufferedImage tag_big = new BufferedImage(new_w_big,new_h_big,BufferedImage.TYPE_INT_RGB);
tag_min.getGraphics().drawImage(src,0,0,new_w_min,new_h_min,null); //绘制缩小后的图
tag_mid.getGraphics().drawImage(src,0,0,new_w_mid,new_h_mid,null); //绘制缩小后的图
tag_big.getGraphics().drawImage(src,0,0,new_w_big,new_h_big,null); //绘制缩小后的图
FileOutputStream newimage_min=new FileOutputStream(newurl_min); //输出到文件流
FileOutputStream newimage_mid=new FileOutputStream(newurl_mid); //输出到文件流
FileOutputStream newimage_big=new FileOutputStream(newurl_big); //输出到文件流
JPEGImageEncoder encoder_min = JPEGCodec.createJPEGEncoder(newimage_min);
JPEGImageEncoder encoder_mid = JPEGCodec.createJPEGEncoder(newimage_mid);
JPEGImageEncoder encoder_big = JPEGCodec.createJPEGEncoder(newimage_big);
encoder_min.encode(tag_min); //近JPEG编码
encoder_mid.encode(tag_mid); //近JPEG编码
encoder_big.encode(tag_big); //近JPEG编码
newimage_min.close();
newimage_mid.close();
newimage_big.close();
}
catch(Exception e){
%>
<script type="text/javascript">
alert("图片太大!");
window.location.href="image_upload_main.jsp";
</script>
<%
}
%>