A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© yangcy 中级黑马   /  2014-7-12 13:53  /  1174 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Date;

  7. import org.directwebremoting.WebContext;
  8. import org.directwebremoting.WebContextFactory;

  9. public class UploadImageUtil {
  10.         private UploadImageUtil() {
  11.         }

  12.         public static String uploadImage(String goodsPhoto) {
  13.                 // 截取图片路径的前面部分,获取图片名称部分
  14.                 goodsPhoto = goodsPhoto.substring(12);
  15.                 String goodsPhotoUrl = goodsPhoto;
  16.                 WebContext webContext = WebContextFactory.get();
  17.                 // 图片存放在服务器上的路径
  18.                 String goodsUrl = webContext.getHttpServletRequest().getSession()
  19.                                 .getServletContext().getRealPath("/images/goodsPhotos");
  20.                 // 获取系统当前时间
  21.                 long nowTime = System.currentTimeMillis();
  22.                 // 定义一个格式化日期对象
  23.                 SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
  24.                 Date date = new Date(nowTime);
  25.                 // 格式化日期
  26.                 String strDate = format.format(date);
  27.                 // 拼接目标地址
  28.                 goodsUrl += "\\" + strDate + ".jpg";
  29.                 FileInputStream in = null;
  30.                 FileOutputStream out = null;
  31.                 try {
  32.                         // 读取文件
  33.                         in = new FileInputStream(goodsPhotoUrl);
  34.                         // 创建文件
  35.                         File file = new File(goodsUrl);
  36.                         // 如果文件不存在
  37.                         if (!file.exists()) {
  38.                                 // 创建一个文件
  39.                                 file.createNewFile();
  40.                         }
  41.                         // 写出文件
  42.                         out = new FileOutputStream(file);
  43.                         int ch = -1;
  44.                         byte[] buffer = new byte[1024];
  45.                         while ((ch = in.read(buffer)) != -1) {
  46.                                 out.write(buffer);
  47.                                 out.flush();
  48.                         }
  49.                         out.flush();
  50.                 } catch (IOException e) {
  51.                         e.printStackTrace();
  52.                 } finally {
  53.                         try {
  54.                                 in.close();
  55.                                 out.close();
  56.                         } catch (IOException e) {
  57.                                 e.printStackTrace();
  58.                         }
  59.                 }
  60.                 // 返回图片在服务上的图片名称
  61.                 return strDate + ".jpg";
  62.         }

  63. }
复制代码
分析:上传图片到tomcat服务器,说白了就是将源路径中的图片资源读取后,写到目的tomcat服务器相应的路径中。

评分

参与人数 1技术分 +2 收起 理由
HM汪磊 + 2 赞一个!

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马