UpdataImage.java
资源名称:文件管理系统.zip [点击查看]
上传用户:kzxteda
上传日期:2007-06-01
资源大小:54k
文件大小:4k
源码类别:
家庭/个人应用
开发平台:
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("rnrn"), 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("rnrn");
- temp=temp.substring(start1+4);
- start2=temp.indexOf(";rn");
- 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;
- }
- }