码迷,mamicode.com
首页 > Web开发 > 详细

上传图片

时间:2014-11-11 20:59:07      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   os   使用   java   sp   for   

********

1,jsp页面

 

2,后台struts的action中

public boolean saveAttach(TBasePhotoAttach photo) throws Exception{
        /*System.out.println(upload1FileName); //flower.jpg
        System.out.println(upload1); 
        System.out.println(upload1ContentType); //image/pjpeg   */

    //只能上传这些格式的图片
if(!"image/jpeg".equals(upload1ContentType) &&!"image/gif".equals(upload1ContentType) &&!"image/pjpeg".equals(upload1ContentType)){ return false; }
    //图片名 String photoName
= upload1FileName.substring(0,upload1FileName.lastIndexOf("."));
    //图片后缀 String suffix
=upload1FileName.substring(upload1FileName.lastIndexOf(".")+1); SimpleDateFormat format=new SimpleDateFormat("yyyyMMddhhmmss"); //使用当前时间为图片命名 String newName=format.format(new Date())+"."+suffix; //用于保存上传照片的文件夹 String uploadPath=this.getRequest().getRealPath("/")+"uploadImages"; // E:JavaWorkspace dp WebRoot uploadImages File file=new File(uploadPath); if(!file.exists()){ file.mkdirs(); } String saveUrl=uploadPath +"\\"+ newName; //完整保存路径 //文件输入流 FileInputStream fis = new FileInputStream(upload1); //文件输出流 FileOutputStream fos = new FileOutputStream(saveUrl, false); byte buffer[] = new byte[4096]; int readSize = 0; //存储文件 while ((readSize = fis.read(buffer)) > 0) { fos.write(buffer, 0, readSize); } //photo.setPhotoUrl(saveUrl); //URLEncoder.encode(upload1FileName, "UTF-8"); //DATE.getCurrentDate().toString() String photoUrl=this.getRequest().getContextPath()+"/uploadImages/"+newName; photo.setPhotoUrl(photoUrl); photo.setOldname(upload1FileName); photo.setNewname(newName); stuService.savePhoto(photo); return true; }

 

*************

上传图片

标签:style   blog   io   color   os   使用   java   sp   for   

原文地址:http://www.cnblogs.com/qingmaple/p/4090324.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!