<%@ page contentType= "text/html;charset=GBK " %>
<%@ page language= "java " import= "com.jspsmart.upload.* "%>
<%@ page import= "java.io.*,java.awt.Image,java.awt.image.*,com.sun.image.codec.jpeg.* " %>
<jsp:useBean id= "mySmartUpload " scope= "page " class= "com.jspsmart.upload.SmartUpload " />
<HTML>
<BODY BGCOLOR= "white ">
<%
//初始化文件名和上传结果
String FileName = "noImage ";
String result = "error ";
try
{
// 执行初始化操作
mySmartUpload.initialize(pageContext);
//限制文件上传格式
mySmartUpload.setAllowedFilesList( "jpg,gif,bmp,jpeg,JPG,BMP,GIF ");
// 上传文件到服务器
mySmartUpload.upload();
//如果有文件上传
if(mySmartUpload.getFiles().getCount()> 0)
{
// 取出文件
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
//
//如果文件存在
if(myFile.getSize()> 0)
{
String strExt = myFile.getFileExt();
java.util.Date d = new java.util.Date();
FileName = d.getTime()+ ". "+strExt;
myFile.saveAs( "../webblog/images/upload/articleImage/ " +FileName);
result= "success ";
//以下生成缩略图
String strRealPath = request.getRealPath( "/ ")+ "//images//upload//articleImage// "+FileName; //原图路径
java.io.File file = new java.io.File(strRealPath); //读入刚才上传的文件
String newUrl=request.getRealPath( "/ ")+ "//images//upload//articleImage//small// "+FileName; //新的缩略图保存地址
Image src = javax.imageio.ImageIO.read(file); //构造Image对象
float tagsize=300;
int old_w=src.getWidth(null); //得到源图宽
int old_h=src.getHeight(null); //得到源图长
int tempsize;
float tempdouble = 1;
if(old_w > 300 || old_h> 300){
if(old_w> old_h){
tempdouble=old_w/tagsize;
}else{
tempdouble=old_h/tagsize;
}
}
int new_w =Math.round(old_w/tempdouble);
int 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
{
System.out.println( "文件不存在! ");
//result= "error ";
}
}
else
{
System.out.println( "没上传! ");
//result= "error ";
}
}
catch(Exception ex)
{
System.out.println(ex);
//result= "error ";
}
finally
{
//存储表单信息
String Atitle = mySmartUpload.getRequest().getParameter( "Atitle ");
String Acontent = mySmartUpload.getRequest().getParameter( "Acontent ");
String Atype = mySmartUpload.getRequest().getParameter( "Atype ");
String Astate = mySmartUpload.getRequest().getParameter( "Astate ");
request.setAttribute( "Atitle ",Atitle);
request.setAttribute( "Acontent ",Acontent);
request.setAttribute( "Atype ",Atype);
request.setAttribute( "Astate ",Astate);
request.setAttribute( "FileName ",FileName);
request.setAttribute( "Result ",result);
}
%>
<jsp:forward page= "../insertarticlesrv "/>
</BODY>
</HTML>
<%@ page language= "java " import= "com.jspsmart.upload.* "%>
<%@ page import= "java.io.*,java.awt.Image,java.awt.image.*,com.sun.image.codec.jpeg.* " %>
<jsp:useBean id= "mySmartUpload " scope= "page " class= "com.jspsmart.upload.SmartUpload " />
<HTML>
<BODY BGCOLOR= "white ">
<%
//初始化文件名和上传结果
String FileName = "noImage ";
String result = "error ";
try
{
// 执行初始化操作
mySmartUpload.initialize(pageContext);
//限制文件上传格式
mySmartUpload.setAllowedFilesList( "jpg,gif,bmp,jpeg,JPG,BMP,GIF ");
// 上传文件到服务器
mySmartUpload.upload();
//如果有文件上传
if(mySmartUpload.getFiles().getCount()> 0)
{
// 取出文件
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
//
//如果文件存在
if(myFile.getSize()> 0)
{
String strExt = myFile.getFileExt();
java.util.Date d = new java.util.Date();
FileName = d.getTime()+ ". "+strExt;
myFile.saveAs( "../webblog/images/upload/articleImage/ " +FileName);
result= "success ";
//以下生成缩略图
String strRealPath = request.getRealPath( "/ ")+ "//images//upload//articleImage// "+FileName; //原图路径
java.io.File file = new java.io.File(strRealPath); //读入刚才上传的文件
String newUrl=request.getRealPath( "/ ")+ "//images//upload//articleImage//small// "+FileName; //新的缩略图保存地址
Image src = javax.imageio.ImageIO.read(file); //构造Image对象
float tagsize=300;
int old_w=src.getWidth(null); //得到源图宽
int old_h=src.getHeight(null); //得到源图长
int tempsize;
float tempdouble = 1;
if(old_w > 300 || old_h> 300){
if(old_w> old_h){
tempdouble=old_w/tagsize;
}else{
tempdouble=old_h/tagsize;
}
}
int new_w =Math.round(old_w/tempdouble);
int 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
{
System.out.println( "文件不存在! ");
//result= "error ";
}
}
else
{
System.out.println( "没上传! ");
//result= "error ";
}
}
catch(Exception ex)
{
System.out.println(ex);
//result= "error ";
}
finally
{
//存储表单信息
String Atitle = mySmartUpload.getRequest().getParameter( "Atitle ");
String Acontent = mySmartUpload.getRequest().getParameter( "Acontent ");
String Atype = mySmartUpload.getRequest().getParameter( "Atype ");
String Astate = mySmartUpload.getRequest().getParameter( "Astate ");
request.setAttribute( "Atitle ",Atitle);
request.setAttribute( "Acontent ",Acontent);
request.setAttribute( "Atype ",Atype);
request.setAttribute( "Astate ",Astate);
request.setAttribute( "FileName ",FileName);
request.setAttribute( "Result ",result);
}
%>
<jsp:forward page= "../insertarticlesrv "/>
</BODY>
</HTML>