fileupload.java

来自「一个论坛程序」· Java 代码 · 共 160 行

JAVA
160
字号
// Decompiled by Jad v1.5.8f. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   FileUpLoad.java

package com.gamvan.tools;

import java.io.*;
import java.util.Dictionary;
import java.util.Hashtable;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

public class FileUpLoad
{

    public FileUpLoad()
    {
    }

    public String getMessage()
    {
        return message;
    }

    public boolean getBea()
    {
        return bea;
    }

    public String getFileName()
    {
        return fileName;
    }

    public String getFilePath()
    {
        return filePath;
    }

    public String getContentType()
    {
        return contentType;
    }

    public void setSavePath(String savePath)
    {
        this.savePath = savePath;
    }

    public String getFieldValue(String fieldName)
    {
        if(fields == null || fieldName == null)
            return null;
        else
            return (String)fields.get(fieldName);
    }

    private void setFileName(String s)
    {
        if(s == null)
            return;
        int pos = s.indexOf("fileName=\"");
        if(pos != -1)
        {
            filePath = s.substring(pos + 10, s.length() - 1);
            pos = filePath.lastIndexOf("\\");
            if(pos != -1)
                fileName = filePath.substring(pos + 1);
            else
                fileName = filePath;
        }
    }

    private void setContentType(String s)
    {
        if(s == null)
            return;
        int pos = s.indexOf("\uFF1A");
        if(pos != -1)
            contentType = s.substring(pos + 2, s.length());
    }

    public void doUpload(HttpServletRequest request)
        throws IOException
    {
        ServletInputStream in = request.getInputStream();
        byte line[] = new byte[128];
        int i = in.readLine(line, 0, 128);
        if(i < 3)
        {
            message = "\u610F\u5916\u9519\u8BEF\uFF0C\u8BF7\u91CD\u65B0\u5C1D\u8BD5\uFF01";
            bea = false;
            return;
        }
        int boundaryLength = i - 2;
        String boundary = new String(line, 0, boundaryLength);
        fields = new Hashtable();
        message = "\u4E0A\u4F20\u5566!" + String.valueOf(in) + "////" + String.valueOf(fields);
        for(; i != -1; i = in.readLine(line, 0, 128))
        {
            String newLine = new String(line, 0, i);
            if(newLine.startsWith("Content-Disposition:form-data;name=\""))
                if(newLine.indexOf("fileName=\"") != -1)
                {
                    String ffname = new String(line, 0, i - 2);
                    setFileName(ffname);
                    if(fileName == null)
                        return;
                    i = in.readLine(line, 0, 128);
                    setContentType(new String(line, 0, i - 2));
                    i = in.readLine(line, 0, 128);
                    i = in.readLine(line, 0, 128);
                    newLine = new String(line, 0, i);
                    PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter((savePath != null ? savePath : " ") + fileName)));
                    for(; i != -1 && !newLine.startsWith(boundary); pw.close())
                    {
                        i = in.readLine(line, 0, 128);
                        if((i == boundaryLength + 2 || i == boundaryLength + 4) && (new String(line, 0, i)).startsWith(boundary))
                        {
                            pw.print(newLine.substring(0, newLine.length() - 2));
                        } else
                        {
                            pw.print(newLine);
                            newLine = new String(line, 0, i);
                        }
                    }

                } else
                {
                    int pos = newLine.indexOf("name=\"");
                    String fieldName = newLine.substring(pos + 6, newLine.length() - 3);
                    i = in.readLine(line, 0, 128);
                    i = in.readLine(line, 0, 128);
                    newLine = new String(line, 0, i);
                    StringBuffer fieldValue = new StringBuffer(128);
                    i = in.readLine(line, 0, 128);
                    if((i == boundaryLength + 2 || i == boundaryLength + 4) && (new String(line, 0, i)).startsWith(boundary))
                    {
                        fieldValue.append(newLine.substring(0, newLine.length() - 2));
                    } else
                    {
                        fieldValue.append(newLine);
                        newLine = new String(line, 0, i);
                    }
                    fields.put(fieldName, fieldValue.toString());
                }
        }

    }

    private String savePath;
    private String filePath;
    private String fileName;
    private String contentType;
    private Dictionary fields;
    public String message;
    private boolean bea;
}

⌨️ 快捷键说明

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