⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 upload.txt

📁 主要实现了在structs1.1中各种文件的上传功能!
💻 TXT
字号:
package com.hywavesoft.struts.upload;

import org.apache.struts.action.ActionForm;

import org.apache.struts.upload.FormFile;


public class UploadForm extends ActionForm {

    private FormFile theFile;

    public FormFile getTheFile() {

        return theFile;

    }

    public void setTheFile(FormFile theFile) {

        this.theFile = theFile;

    }

}

package com.hywavesoft.struts.upload;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;


public abstract class BaseAction extends Action {

    protected static final String SAVE = "save";

    protected static final String DELETE = "delete";

    protected static final String DOWN = "DOWN";

    protected static final String DATABASE_DEST = "database";

    protected static final String FILE_DEST = "file";

    public abstract ActionForward execute(

        ActionMapping mapping,

        ActionForm form,

        HttpServletRequest request,

        HttpServletResponse response) throws Exception;


    public String getPath(String filePath){

        String path = getServlet().getServletContext().getRealPath(filePath) + "\\";
        return path;
    }

}


/**
 * 这是一个辅助类,辅助完成上传功能。
 * 可以选择将文件保存在数据库里或保存在文件系统上
 * 并对文件的类型和大小进行了限制
 */

package com.hywavesoft.struts.commons;

import java.io.*;

public class UploadUtil {

  private static final String DATABASE_DEST = "database";

  private static final String FILE_DEST = "file";

  private static final int MAX_SIZE = 1024 * 1024;

  private static final String[] TYPES = {
      ".jpg", ".gif", ".zip", ".rar", ".doc"};

  public static void saveFile(String fileName, byte[] fileData, int size,

                              String dest) throws FileNotFoundException,
      IOException {

    /*  if (!checkSize(size)) {
        throw new IOException(size + " is too large !");
      }*/

    if (!checkType(fileName)) {

      throw new IOException("Unvaildate type !");

    }

    if (dest.equals(DATABASE_DEST)) {

      saveToDb(fileName, fileData);

    }

    if (dest.equals(FILE_DEST)) {

      saveToFile(fileName, fileData);

    }

  }

  private static void saveToDb(String fileName, byte[] fileData) {

  }

  private static void saveToFile(String fileName, byte[] fileData)

      throws FileNotFoundException, IOException {

    OutputStream o = new FileOutputStream(fileName);

    o.write(fileData);

    o.close();

  }

  public static void delFile(String fileName, String dest)

      throws NullPointerException, SecurityException {

    if (dest.equals(DATABASE_DEST)) {

      delFromDb(fileName);

    }

    if (dest.equals(FILE_DEST)) {

      delFromFile(fileName);

    }

  }

  private static void delFromDb(String fileName) {

  }

  private static void delFromFile(String fileName)

      throws NullPointerException, SecurityException {

    File file = new File(fileName);

    if (file.exists()) {

      file.delete();

    }
  }

  private static boolean checkSize(int size) {

    if (size > MAX_SIZE) {

      return false;
    }

    return true;

  }

  private static boolean checkType(String fileName) {

    for (int i = 0; i < TYPES.length; i++) {

      if (fileName.toLowerCase().endsWith(TYPES[i])) {

        return true;

      }

    }

    return false;

  }

  public static  String getContentType(String fileName) {
    String fileNameTmp = fileName.toLowerCase();
    String ret = "";
    if (fileNameTmp.endsWith("txt")) {
      ret = "text/plain";
    }
    if (fileNameTmp.endsWith("gif")) {
      ret = "image/gif";
    }
    if (fileNameTmp.endsWith("jpg")) {
      ret = "image/jpeg";
    }
    if (fileNameTmp.endsWith("jpeg")) {
      ret = "image/jpeg";
    }
    if (fileNameTmp.endsWith("jpe")) {
      ret = "image/jpeg";
    }
    if (fileNameTmp.endsWith("zip")) {
      ret = "application/zip";
    }
    if (fileNameTmp.endsWith("rar")) {
      ret = "application/rar";
    }
    if (fileNameTmp.endsWith("doc")) {
      ret = "application/msword";
    }
    if (fileNameTmp.endsWith("ppt")) {
      ret = "application/vnd.ms-powerpoint";
    }
    if (fileNameTmp.endsWith("xls")) {
      ret = "application/vnd.ms-excel";
    }
    if (fileNameTmp.endsWith("html")) {
      ret = "text/html";
    }
    if (fileNameTmp.endsWith("htm")) {
      ret = "text/html";
    }
    if (fileNameTmp.endsWith("tif")) {
      ret = "image/tiff";
    }
    if (fileNameTmp.endsWith("tiff")) {
      ret = "image/tiff";
    }
    if (fileNameTmp.endsWith("pdf")) {
      ret = "application/pdf";
    }
    return ret;
  }

}



package com.hywavesoft.struts.upload;
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 java.io.*;
import java.net.URLEncoder;
import com.hywavesoft.struts.commons.UploadUtil;
public class downAction
    extends BaseAction {
  public ActionForward execute(ActionMapping mapping, ActionForm form,
                               HttpServletRequest request,
                               HttpServletResponse response) throws Exception {
    UploadForm uploadForm = (UploadForm) form;
    String fileName = request.getParameter("filename");
    String sysroot = servlet.getServletContext().getInitParameter("sysroot");
    File file = new File(getPath(sysroot) + fileName);//
 if(file.exists()){
  try{
   BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
   byte[] buffer = new byte[1024];
   fileName = java.net.URLEncoder.encode(fileName, "UTF-8");//处理中文文件名的问题
   fileName = new String(fileName.getBytes("UTF-8"),"GBK");//处理中文文件名的问题
   response.reset();
 //  response.setCharacterEncoding("UTF-8");
  // response.setContentType("application/x-rar-compressed");//不同类型的文件对应不同的MIME类型
  response.setContentType(UploadUtil.getContentType(fileName));
   response.setHeader("Content-Disposition","attachment; filename=" + fileName);
   OutputStream os = response.getOutputStream();
   while(bis.read(buffer) > 0){
    os.write(buffer);
   }
   bis.close();
   os.close();
  }
  catch (IOException e) {
          System.err.print(e);
 }
 }

  /*    BufferedInputStream bis = null;
      BufferedOutputStream bos = null;
      OutputStream fos = null;
      InputStream fis = null;

      try {
        response.setContentType(UploadUtil.getContentType(fileName));
        response.setHeader("Content-disposition", "attachment;filename="
                           + URLEncoder.encode(fileName, "utf-8"));
      //  response.setHeader("Content-Disposition","attachment; filename=" + fileName);

        fis = new FileInputStream(getPath(sysroot) + fileName);
        bis = new BufferedInputStream(fis);
        fos = response.getOutputStream();
        bos = new BufferedOutputStream(fos);

        int bytesRead = 0;
        byte[] buffer = new byte[5 * 1024];
        while ( (bytesRead = bis.read(buffer)) != -1) {
          bos.write(buffer, 0, bytesRead); //将文件发送到客户端
        }
      }
      catch (IOException e) {
//              response.setContentType("text/html");
        response.reset();
        //设置文件物理下载时出现的错误信息
        // this.setSysMessage(request, "download.failed", "btn.reupload","FileUpload.do?act=showFile");
        return mapping.findForward("error");
      }
      finally {
        try {
          if (fos != null) {
            fos.close();
          }
          if (bos != null) {
            bos.close();
          }
          if (fis != null) {
            fis.close();
          }
          if (bis != null) {
            bis.close();
          }
        }
        catch (IOException e) {

          System.err.print(e);
        }
      }*/
    return mapping.findForward("success");

  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -