根据struts的FormFile来上传文件

编程技术  /  houtizong 发布于 3年前   69

package com.score.web.action.picture;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.score.model.members.ScoreMembers;
import com.score.model.picture.ScorePhoto;
import com.score.service.exception.BeanAlreadyExistsException;
import com.score.service.picture.ScorePhotoManager;
import com.score.web.action.core.BaseAction;
import com.score.web.action.upload.UploadFileForm;

public class CreatePhotoAction extends BaseAction{
  public ActionForward execute(ActionMapping mapping, ActionForm actionForm,
    HttpServletRequest request, HttpServletResponse response) {
 
    UploadFileForm uploadForm = (UploadFileForm) actionForm;
    FormFile file = uploadForm.getFileForm();
    
    if(file.getContentType().equals("image/jpeg")){ //判断类型
     System.out.println("jpeg 类型");
    }else if(file.getContentType().equals("image/png")){
     System.out.println("png 类型");
    }else if(file.getContentType().equals("image/gif")){
     System.out.println("gif 类型");
    }else if(file.getContentType().equals("image/pjpeg")){
     System.out.println("jpg 类型");
    }else{
     System.out.println("不是图片!");
     request.setAttribute("errorFlag", "1");
     return mapping.findForward("listpicture");
    }
   
         if(file.getFileSize()>2097152){ //判断大小
     System.out.println("大于2M");
     request.getSession().setAttribute("errorFlag",2);
     return mapping.findForward("listpicture");
    }
    
    String uri = membersHead(request,file);
    request.setAttribute("uri", uri);
    
    ScorePhotoManager spm=(ScorePhotoManager)getBean("scorePhotoManager");
    ScorePhoto sp=new ScorePhoto();
    sp.setPhotoAddr(uri);
    try {
     spm.createScorePhoto(sp);
    } catch (BeanAlreadyExistsException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    request.getSession().setAttribute("errorFlag",1);
    return mapping.findForward("listpicture");
   }

//membersHead方法可以写到service层,然后调用,此处为方便理解,写在action中

//membersHead方法判断文件的大小和类型

  // 会员资料文件
  public String membersHead(HttpServletRequest request,FormFile file) {
   String uri = "";
   String dir = servlet.getServletContext().getRealPath("/upload");
   // 文件夹
   File dirFile = null;
   boolean bFile = false;
   dirFile = new File(dir);
   bFile = dirFile.exists();// true 该文件夹存在,false 不存在
   if (bFile == false) {
    bFile = dirFile.mkdirs();
    if (bFile == false) {
     System.out.println(" 文件夹创建失败!");
    }
   }

   if (bFile == true) {
    try {

     InputStream input = file.getInputStream();// 能从FormFile中获得输入流
     String uid = UUID.randomUUID().toString();
     String fileName = file.getFileName();
     fileName = uid + fileName.substring(fileName.lastIndexOf("."));
     OutputStream output = new FileOutputStream(dir + "/" + fileName);

     int bytesReader = 0;

     byte[] readbuffer = new byte[102400];

     while ((bytesReader = input.read(readbuffer, 0, 102400)) != -1) {

      output.write(readbuffer, 0, bytesReader);

     }
     output.close();
     String protco = request.getProtocol().substring(0,
       request.getProtocol().indexOf("/"));
     String webPath = protco + "://" + request.getServerName() + ":"
       + request.getServerPort();
     // 资料路径
     uri = webPath + "/upload/"+ fileName;

    } catch (Exception e) {
     e.printStackTrace();
    }
   }
   return uri;
   

  }
  
 }
附:

/**
 * 所要上传的文件的Form
 * @author UploadFileForm 
 * @version $Revision: $ $Date: $
 */
public class UploadFileForm extends BaseForm implements java.io.Serializable{
 
 private FormFile file;

 public FormFile getFile() {
  return file;
 }

 public void setFile(FormFile file) {
  this.file = file;
 }

}

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客