📄 pictureadmin.java
字号:
/* * PictureAdmin.java * * Created on 2004年12月23日, 上午10:24 * 这个类主要用于管理用户相片 * */package com.mg.admin;import java.io.*;import java.util.*;import org.jdom.*;import org.jdom.input.*;import org.jdom.output.*;import javax.servlet.http.HttpServletRequest;import java.awt.image.BufferedImage;import javax.swing.ImageIcon;import java.awt.Graphics2D;import java.awt.RenderingHints;import javax.imageio.ImageIO;/** * 这个类主要用于管理用户相片 */public class PictureAdmin extends Object{ //********************常量************************ /** * 相片的上传目录 */ public static final String UP_LOAD_FOLDER="upload_img"; /** * 缩略图的宽 */ public static final int SMALL_WIDTH = 128; /** * 缩略图的高 */ public static final int SMALL_HEIGHT = 128; //********************成员变量************************ /** * 操作的根路径 */ private String rootPath; /** * 当前操作的XML文件名 */ private static final String TABLE_NAME = "pictures.xml"; //********************成员函数************************ /** * 构造函数,如果使用这个构造函数,需要调用setRootPath设置根路径 * <p> * @see #setRootPath */ public PictureAdmin() { } /** * 设置操作的根路径 * <p> * @see #getRootPath */ public void setRootPath(String rp) { if (rootPath != rp) { rootPath = rp; } } /** * 返回操作的根路径 * <p> * @return 返回根路径 * <p> * @see #setRootPath */ public String getRootPath() { return rootPath; } /** * 获得当前的XML Document对象 */ private Document getDoc() { Document doc = null; PrintStream out = System.out; //读取文件 try { File file = new File(rootPath, TABLE_NAME); if (file.exists()) { SAXBuilder builder = new SAXBuilder(false); doc = builder.build(file); } } catch(Exception e) { out.println("Exception: " + e.toString()); } return doc; } /** * 连接当前的rootPath下的pictures.xml名的XML文件, * 并返回当前用户的所有的相片。 * <p> * @param uid 当前用户的ID。 * <p> * @return 封装了Picture对象的Vecotr对象。 */ public Vector getPictures(String uid) { Vector ret = new Vector(); Document doc=getDoc(); if (doc != null) { // 获取根节点 Element root = doc.getRootElement(); List list = root.getChildren(); Iterator list_i = list.iterator(); Element userNode=null; while (list_i.hasNext()) { //获取子节点 Element e = (Element)list_i.next(); //获取二级子节点 List details = e.getChildren(); if (details.size()<Picture.ELEMENT_NUM) { //错误 break; } //添加Picture对象 String text1 = ((Element)details.get(0)).getText(); String text2 = ((Element)details.get(1)).getText(); String text3 = ((Element)details.get(2)).getText(); String text4 = ((Element)details.get(3)).getText(); if (!text4.equals(uid)) { continue; } Picture picture = new Picture(); picture.setTitle(text1); picture.setFileName(text2); picture.setDateTime(text3); picture.setUser(text4); ret.add(picture); } } return ret; } /** * 连接当前的rootPath下的pictures.xml名的XML文件, * 并添加一个相片,如果成功则返回true,否则返回false。 * <p> * @param message Message对象 */ public boolean add(Picture picture) { boolean ret = false; Document doc=getDoc(); if (doc != null) { // 获取根节点 Element root = doc.getRootElement(); Element newE = new Element("picture"); Element eTitle = new Element("title"); eTitle.setText(picture.getTitle()); newE.addContent(eTitle); Element eContent = new Element("content"); eContent.setText(picture.getFileName()); newE.addContent(eContent); Element eDateTime = new Element("datetime"); eDateTime.setText(picture.getDateTime()); newE.addContent(eDateTime); Element eUserID = new Element("userid"); eUserID.setText(picture.getUser()); newE.addContent(eUserID); root.addContent(newE); //保存修改 save(doc); ret = true; } return ret; } /** * 连接当前的rootPath下的pictures.xml名的XML文件, * 并删除相片,如果成功则返回true,否则返回false。 * <p> * @param picture Picture */ public boolean delete(Picture picture) { boolean ret = false; Document doc=getDoc(); if (doc != null) { // 获取根节点 Element root = doc.getRootElement(); List list = root.getChildren(); Iterator list_i = list.iterator(); Element userNode=null; while (list_i.hasNext()) { //获取子节点 Element e = (Element)list_i.next(); //获取二级子节点 List details = e.getChildren(); if (details.size()<Message.ELEMENT_NUM) { //错误 return ret; } //判断是否相等 //title String text1 = ((Element)details.get(0)).getText(); //filename String text2 = ((Element)details.get(1)).getText(); //datetime String text3 = ((Element)details.get(2)).getText(); //user String text4 = ((Element)details.get(3)).getText(); if (!text1.equals(picture.getTitle()) || !text2.equals(picture.getFileName()) || !text3.equals(picture.getDateTime()) || !text4.equals(picture.getUser())) { //如果不相等则继续查找 continue; } else { //删除当前节点 deleteFile(rootPath + UP_LOAD_FOLDER + "/" + text4, text2); root.removeContent(e); break; } } //保存修改 save(doc); ret = true; } return ret; } private void deleteFile(String root, String fn) { try { File file = new File(fn); if (file.exists()) { file.delete(); } }catch(Exception e) { System.out.println("Delete File Exception: " + e.toString()); } } /** * 连接当前的rootPath下的pictures.xml名的XML文件, * 并删除相片,如果成功则返回true,否则返回false。 * <p> * @param filename 要删除的相片文件名 */ public boolean delete(String filename) { boolean ret = false; Document doc=getDoc(); if (doc != null) { // 获取根节点 Element root = doc.getRootElement(); List list = root.getChildren(); Iterator list_i = list.iterator(); Element userNode=null; while (list_i.hasNext()) { //获取子节点 Element e = (Element)list_i.next(); //获取二级子节点 List details = e.getChildren(); if (details.size()<Picture.ELEMENT_NUM) { //错误 return ret; } //filename String fn = ((Element)details.get(1)).getText(); String uid = ((Element)details.get(3)).getText(); if (!fn.equals(filename)) { //如果不相等则继续查找 continue; } else { //删除当前节点 deleteFile(rootPath + UP_LOAD_FOLDER + "/" + uid, fn); root.removeContent(e); break; } } //保存修改 save(doc); ret = true; } return ret; } /** * 上传相片 * <p> * @param req HttpServletRequest对象,里面包含了上传信息。 * @param sizeMax 最大文件限制。 * @param targetpath 保存的目标根路径,在该目录下,文件将保存在用户自己的文件夹中。 * @param uid 当前用户ID。 * @param title 图片的标题。 * <p> * @return 如果保存成功返回true,否则返回false。 */ public boolean upload(HttpServletRequest req, long sizeMax, String targetPath, String uid) { boolean ret = false; try { FileUpload fu = new FileUpload(); List fileItems = fu.parseRequest(req,sizeMax,targetPath); if (fileItems.size()<1) { return ret; } Iterator i = fileItems.iterator(); while (i.hasNext()) { //String comment = ((Picture)i.next()).getString(); Picture picture = (Picture)i.next(); if (!picture.isValidFileType()) { //如果文件类型错误,退出 continue; } //不保存其他域中的内容 String clientSideName = picture.getClientShortName(); if (clientSideName == "") { continue; } String serverSideName = getFileName(uid, clientSideName); //picture.setTitle(title); picture.setUser(uid); picture.setFileName(serverSideName); File path = new File(targetPath + "\\" + uid); if (!path.exists()) { //如果目录不存在,就创建目录 path.mkdir(); } File file = new File(path, serverSideName); picture.write(file); add(picture); } ret = true; }catch (Exception e) { System.out.println("upload exception: " + e.toString()); } return ret; } /** * 根据用户ID,当前时间和原始文件名,创建新的文件名。 */ private String getFileName(String uid, String fn) { Date rightNow = new Date(); return uid + "_" + String.valueOf(rightNow.getTime()) + "_" + fn; } /** * 保存修改到XML文件 * <p> * @param doc 要保存的Document对象 */ private void save(Document doc) { //保存修改 try { Format format = Format.getPrettyFormat(); format.setEncoding("GBK"); XMLOutputter out = new XMLOutputter(format); File file = new File(rootPath, TABLE_NAME); FileOutputStream fos = new FileOutputStream(file); out.output(doc, fos); } catch(Exception e) { System.out.println("Exception: " + e.toString()); } } public boolean createSmallPicture(Picture picture, String path, String uid) { File fullpath = new File(path + "small\\"); if (!fullpath.exists()) { fullpath.mkdir(); } //如果缩小的文件存在则返回true File smallFile = new File(fullpath, picture.getFileName()); if (smallFile.exists()) { return true; } //如果原文件不存在则返回false File file = new File(path, picture.getFileName()); if (!file.exists()) { return false; } ImageIcon ii = new ImageIcon(path + picture.getFileName()); BufferedImage bi = new BufferedImage(SMALL_WIDTH, SMALL_HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = bi.createGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.drawImage(ii.getImage(), 0, 0, SMALL_WIDTH, SMALL_HEIGHT, null); String type = picture.getFileType(); if (type!=""){ //保存文件 try { ImageIO.write(bi, type, smallFile); } catch(Exception e) { System.out.println("保存文件时出现异常:" + e.toString()); return false; } } return true; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -