上传图片

********

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; }

 

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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。