|
[PHP]
package com.ljsilver.photoadmin;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.io.*;
import java.util.Calendar;
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.awt.image.AffineTransformOp;
import javax.imageio.ImageIO;
import java.awt.geom.AffineTransform;
import com.jspsmart.upload.*;
import com.ljsilver.util.DatabaseOperate;
import com.ljsilver.util.HTMLString;
public class UpClassPhoto extends HttpServlet {
private ServletConfig config=null;
private ServletContext application;
public void init(ServletConfig config)
{
this.config = config;
try
{
application=config.getServletContext();
}
catch(Exception E)
{
System.out.println("Init Error:"+E);
}
}
public void destroy(){
config = null;
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException{
request.setCharacterEncoding("GBK");
response.setContentType("text/html; charset=GBK");
PrintWriter out=response.getWriter();
HttpSession session=request.getSession();
try{
String userName = (String)session.getAttribute("userName");
if(userName == null){
response.sendRedirect("/login.jsp?error=1");
}
}catch(Exception ex){
response.sendRedirect("/login.jsp?error=1");
}
String groupIdStr = session.getAttribute("groupid")==null?"0"String)session.getAttribute("groupid");
int groupId = Integer.parseInt(groupIdStr);
if(groupId==0){
response.sendRedirect("/grouplist.jsp?error=1");
}
Integer integer = (Integer)session.getAttribute("userId");
int userId = integer.intValue();
SmartUpload mySmartUpload = new SmartUpload();
com.jspsmart.upload.File myFile =null;
try {
// Initialization
mySmartUpload.initialize(this.config,request,response);
mySmartUpload.setAllowedFilesList("bmp,BMP,Bmp,bMp,bmP,BMp,BmP,bMP,"
+"gif,GIF,Gif,gIf,giF,gIG,GiF,GIf,"
+"jpg,JPG,JPg,JpG,jPG,Jpg,jPg,jpG,"
+"jpeg,JPEG,Jpeg,jPeg,jpEg,jpeG,"
+"jPEG,JpEG,JPeG,JPEg,JPeg,JpEg,JpeG,"
+"jPEg,jPeG,jpEG,"
+"png,Png,pNg,pnG,pNG,PnG,PNg,PNG");
mySmartUpload.setMaxFileSize(10*1024*1024);
// Upload
mySmartUpload.upload();
if(mySmartUpload.getFiles().getCount()==0) {
response.sendRedirect("/error.jsp?error=9");
return;
}
myFile = mySmartUpload.getFiles().getFile(0);
Calendar rightNow = Calendar.getInstance();
String smallFileName = ""+userId+"-"+rightNow.getTimeInMillis();
String fileName = smallFileName+"."+myFile.getFileExt();
String smallExtName = "";
if("jpg,JPG,JPg,JpG,jPG,Jpg,jPg,jpG,".indexOf(myFile.getFileExt())==-1){
smallExtName += "png";
}else{
smallExtName += "jpg";
}
smallFileName += "-s."+smallExtName;
String size = ""+myFile.getSize()/1024+"K";
String photoIntro = mySmartUpload.getRequest().getParameter("intro")==""?"无说明":mySmartUpload.getRequest().getParameter("intro");
myFile.saveAs(application.getRealPath("/classphoto/")+fileName,mySmartUpload.SAVE_PHYSICAL);
java.io.File bigFile= new java.io.File(application.getRealPath("/classphoto/")+fileName);
java.io.File smallFile=new java.io.File(application.getRealPath("/classphoto/")+smallFileName);
BufferedImage bigFileImage = ImageIO.read(bigFile);
int height = bigFileImage.getHeight();
int width = bigFileImage.getWidth();
int smallHeight = 0;
int smallWidth = 0;
if(height>=width){
smallHeight = 100;
smallWidth = width*100/height;
}else{
smallHeight = height*100/width;
smallWidth = 100;
}
double heightRatio = (double)smallHeight/height;
double widthRatio = (double)smallWidth/width;
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(widthRatio, heightRatio), null);
Image smallPhoto = op.filter(bigFileImage, null);
try {
ImageIO.write((BufferedImage)smallPhoto,smallExtName,smallFile);
}catch (Exception ex) {
System.out.println(" ImageIo.write error in CreatThum.: "+ex.getMessage());
}
String ip = request.getRemoteAddr();
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");
String time = sdf.format(java.util.Calendar.getInstance().getTime());
String ratio = ""+(int)width+"x"+(int)height;
String sqlStr = "insert into classphoto(userid,photointro,biglink,smalllink,time,ip,ratio,volume,groupid) values(?,?,?,?,?,?,?,?,?)";
DatabaseOperate db = new DatabaseOperate(sqlStr);
db.setInt(1,userId);
db.setBytes(2,HTMLString.HTMLBR(photoIntro).getBytes("GBK"));
db.setString(3,"/classphoto/"+fileName);
db.setString(4,"/classphoto/"+smallFileName);
db.setString(5,time);
db.setString(6,ip);
db.setString(7,ratio);
db.setString(8,size);
db.setInt(9,groupId);
db.executeSql();
if(db.getDirty()){
out.println("<html><head><title>上传成功</title><meta http-equiv=\"refresh\" content="
+"\"4;URL=/classphotolist.jsp\"></head><body>"
+"上传照片成功,您的照片已经提交!</body></html>");
}else{
out.println("<html><head><title>上传失败</title><meta http-equiv=\"refresh\" content="
+"\"4;URL=/classphotolist.jsp\"></head><body>"
+"上传照片失败,请重试!</body></html>");
}
db.close();
}catch (Exception e){
out.println("<html><head><title>上传失败</title><meta http-equiv=\"refresh\" content"
+"=\"4;URL=/classphotolist.jsp\"></head><body>"
+"上传照片失败,请重试!<br>错误: " + e.toString()+"</body></html>");
}
}
}
[/PHP] |
|