现在的位置: 首页 > 综合 > 正文

Struts实现文件上传(commons-fileupload.jar)

2013年08月30日 ⁄ 综合 ⁄ 共 2833字 ⁄ 字号 评论关闭
/**
 * @作者:Jcuckoo
 * @日期:2008-11-8
 * @版本:V 1.0
 */

对应页面

    <form action="upload.do" method="post" enctype="multipart/form-data">
     <input type="file" name="fileName">
     <input type="submit" value="提交">
    </form>

struts-config.xml
    <form-bean name="uploadForm" type="com.stditbase.struts.form.UploadForm" />
    <!-- 文件上传测试 -->
    <action
      attribute="uploadForm"
      input="/upload.jsp"
      name="uploadForm"
      path="/upload"
      scope="request"
      type="com.stditbase.struts.action.UploadAction" >
      <forward name="success" path="/success.jsp" />
      <forward name="fail" path="/fail.jsp" />
      </action>

UploadForm

public class UploadForm extends ActionForm {
 private FormFile fileName;
 public ActionErrors validate(ActionMapping mapping,
   HttpServletRequest request) {
  return null;
 }
 public void reset(ActionMapping mapping, HttpServletRequest request) {
 }
 public FormFile getFileName() {
  return fileName;
 }
 public void setFileName(FormFile fileName) {
  this.fileName = fileName;
 }
}

UploadAction.java

public class UploadAction extends Action {
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  UploadForm uploadForm = (UploadForm) form;
  String dir = servlet.getServletContext().getRealPath("/newsphoto");
  String tmp=FileUpLoad.saveObject(uploadForm.getFileName(),dir);
  if(tmp!=null){
   return mapping.findForward("success");
  }else{
   return mapping.findForward("fail");
  }
 }
}

FileUpLoad.java

public class FileUpLoad {
 public static String saveObject(FormFile file,String dir){
  if (file.getFileName() == null || file.getFileName().equals("")) {
   return null;
  } else {
   String imageType[] = { "JPG", "jpg", "gif", "bmp", "BMP","pjpeg"};
   String fileType = file.getContentType();
   int i = fileType.indexOf("/");
   fileType = fileType.substring(i + 1);
   for (int j = 0; j < imageType.length; j++) {
    if (imageType[j].equals(fileType)) {
     String path;
     try {
      // 调用图片的上传的方法,并且返回上传服务器的路径
      path = upload(dir, file);
      path = "newsphoto/" + path;
      return path;
     } catch (Exception e) {
      e.printStackTrace();
      return null;
     }
    }
   }
  }
  return null;
 }
 public static String upload(String dir, FormFile formFile) throws Exception {
  Date date = new Date();
  String fname = formFile.getFileName();
  //取文件的后缀
  int i = fname.indexOf(".");
  String type = fname.substring(i + 1);
  //用时间给文件赋新名称
  String name = String.valueOf(date.getTime());
  fname = name + "." + type;
  InputStream streamIn = formFile.getInputStream(); // 创建读取用户上传文件的对象
  File uploadFile = new File(dir); // 创建把上传数据写到目标文件的对象
   // 判断指定路径是否存在,不存在则创建路径
  if (!uploadFile.exists() || uploadFile == null) {
   uploadFile.mkdirs();
  }
  String path = uploadFile.getPath() + "/" + fname;
  OutputStream streamOut = new FileOutputStream(path);
  int bytesRead = 0;
  byte[] buffer = new byte[8192];
  while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
   streamOut.write(buffer, 0, bytesRead);
  }
  streamOut.close();
  streamIn.close();
  formFile.destroy();
  return fname;
 }
}

抱歉!评论已关闭.