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

📄 updataimage.java

📁 文件管理系统,是操作系统课程设计的参考材料.希望大家喜欢.
💻 JAVA
字号:
/////////////////////////////////////
//
//  文章发布系统上传文件方法
//  UpdataImage()
//
//  createFileName()用于文件的存储,并确认
//  doSave()用于确认文件的存储
//  setPath()用于设置路径
//  所有的参数都有自己的set() get() 方法
//
/////////////////////////////////////
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;

public class UpdataImage {

  private ServletRequest request;
  private ServletResponse response;
  private ServletConfig config;
  private ServletInputStream DATA;
  int formSize;
  File f1;
  FileOutputStream outS;
  DataInputStream is;
  String filename;
  String firstName;
  String path;
  byte[] b;
  byte t;
  boolean flag = false;

  public UpdataImage() {
  }
  public void initParam(PageContext context) throws IOException {
    request = context.getRequest();
    response = context.getResponse();
    config = context.getServletConfig();
    DATA = request.getInputStream();
    formSize = request.getContentLength();
  }
  /*
  public boolean setFileName(String s) {
    try {
      File f1 = new File(s);
      outS = new FileOutputStream(f1);
    }
    catch(IOException e) {
      return false;
    }
    return true;
  }
  */
  private void getByte()
  {
    int i=0;
    try
    {
      is=new DataInputStream(DATA);
      b=new byte[formSize];
      while (true)
      {
        try
        {
          t=is.readByte();
          b[i]=t;
          i++;
        }
        catch(EOFException e)
        {
          break;
        }
      }
      is.close();
    }
    catch(IOException e)
    {}
  }

  private boolean createFileName(String firstName,String lastName) {
    try {
      File f1 = new File(path,firstName + lastName);
      if(f1.exists()){}else{
      outS = new FileOutputStream(f1);}
    }
    catch(Exception e) {
      return false;
    }
    return true;
  }
  public boolean doSave()
  {
    int i=0,start1=0,start2=0;
    String temp="";
    if (!flag)
    {
      getByte();
      flag=true;
    }
    try
    {
      temp=new String(b, "ISO8859_1");
      int filint, typeint;
      filint = temp.indexOf("filename=");
      typeint = temp.indexOf("Content-Type:");
      System.out.println(filint);
      String fnl = temp.substring(filint, typeint);
      firstName = fnl.substring(fnl.lastIndexOf("\\"), fnl.lastIndexOf("."));
      String lastName = fnl.substring(fnl.lastIndexOf("."), fnl.lastIndexOf("\""));
      System.out.println(firstName+lastName);
      createFileName(firstName,lastName);
      temp = (temp.substring(temp.indexOf("\r\n\r\n"), temp.length()));
      temp = temp.substring(4, temp.lastIndexOf("-----------------------------"));
      temp = temp.substring(0, temp.lastIndexOf("-----------------------------")-2);
    }
    catch(Exception e)
    {  return(false);}
    try {
      byte[] newbyte = temp.getBytes("ISO8859_1");
      for(i = 0; i < newbyte.length; i++) {
        outS.write(newbyte[i]);
      }
      outS.close();
    }
    catch(Exception e) {
      return false;
    }
    /*
    start1=temp.indexOf("image/");
    temp=temp.substring(start1);

    start1=temp.indexOf("\r\n\r\n");

    temp=temp.substring(start1+4);
    start2=temp.indexOf(";\r\n");
    if (start2!=-1)
    {
      temp=temp.substring(0,start2);
    }
    try
    {
      byte[] img=temp.getBytes("ISO8859_1");
      for (i=0;i<img.length;i++)
             { outS.write(img[i]); }
      outS.close();
    }
    catch(IOException e)
        {return(false);}
    */
    return(true);

  }
  public String getFilename() {
    return filename;
  }
  public void setFilename(String filename) {
    this.filename = filename;
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
  public String getPath() {
    return path;
  }
  public void setPath(String path) {
    this.path = path;
  }
}

⌨️ 快捷键说明

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