📄 postfiles.java
字号:
package com.bookstore.util;
import java.io.*;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
public class PostFiles
{
private String coding;
private String ContentType;
private String boundary;
ServletInputStream inStream;
private byte array1[];
private byte array2[];
private int count1[];
private int count2[];
private int BUFLENGTH;
private String loadFromEntryName;
public PostFiles(String contentType, String coding, ServletInputStream inStream, String entryName)
{
boundary = "";
BUFLENGTH = 5120;
if(entryName != null && entryName.trim().length() > 0)
loadFromEntryName = entryName;
else
loadFromEntryName = "loadFrom";
this.inStream = inStream;
ContentType = contentType;
int i;
if((i = ContentType.indexOf("boundary=")) != -1)
{
boundary = ContentType.substring(i + 9);
boundary = "--" + ContentType;
}
if(coding == null)
coding = "GBK";
this.coding = coding;
array1 = new byte[BUFLENGTH];
array2 = new byte[BUFLENGTH];
count1 = new int[1];
count2 = new int[1];
}
public HashMap getPostParam()
throws IOException
{
HashMap h = new HashMap();
int index = 0;
String str;
while((str = readLine(inStream, array1, count1)) != null)
{
index = str.indexOf("filename=");
if(index >= 0)
{
str = shapeFileName(str);
h.put(loadFromEntryName, str);
break;
}
index = str.indexOf("name=");
if(index >= 0)
{
String paraName = str.substring(index + 6);
int len = paraName.length();
paraName = paraName.substring(0, len - 3);
str = readLine(inStream, array1, count1);
str = readLine(inStream, array1, count1);
len = str.length();
String paraValue = str.substring(0, len - 2);
h.put(paraName, paraValue);
}
}
return h;
}
private String shapeFileName(String str)
{
if(str == null)
return null;
int index = str.indexOf("filename=");
String loadFrom0 = str.substring(index + 10);
int jj = loadFrom0.indexOf("\"");
if(jj > 0)
loadFrom0 = loadFrom0.substring(0, jj);
return loadFrom0;
}
public boolean savePostFile(String absSaveToPath, boolean cover)
throws ServletException, IOException
{
File file = new File(absSaveToPath);
if(!cover && file.exists())
return false;
if(cover && file.exists())
file.delete();
savePostFile(absSaveToPath);
return true;
}
public void savePostFile(String absSaveToPath)
throws ServletException, IOException
{
String s1 = null;
String s2 = null;
File file = null;
try
{
file = new File(absSaveToPath);
File pf = file.getParentFile();
pf.mkdirs();
}
catch(Exception e)
{
e.printStackTrace();
}
FileOutputStream output = new FileOutputStream(file);
s1 = readLine(inStream, array1, count1);
System.out.println("s1=="+s1);
if(s1!=null && s1.indexOf("Content-Type") >= 0)
readLine(inStream, array1, count1);
do
{
s1 = readLine(inStream, array1, count1);
if(s1 == null)
{
break;
}
if(s1.indexOf(boundary) == 0 && array1[0] == 45)
{
break;
}
if(s2 != null)
{
output.write(array2, 0, count2[0]);
output.flush();
s2 = null;
}
s2 = readLine(inStream, array2, count2);
if(s2 == null)
{
break;
}
if(s2.indexOf(boundary) == 0 && array1[0] == 45)
{
break;
}
output.write(array1, 0, count1[0]);
output.flush();
s1 = null;
} while(true);
String cr = System.getProperty("line.separator");
int j = cr.length() != 1 ? 1 * cr.length() : 2 * cr.length();
count2[0] -= j;
count1[0] -= j;
if(s2 != null && array2[0] != 45 && count2[0] > 0)
output.write(array2, 0, count2[0]);
if(s1 != null && array1[0] != 45 && count1[0] > 0)
output.write(array1, 0, count1[0]);
output.close();
}
private String readLine(ServletInputStream inStream, byte array1[], int count1[])
throws IOException
{
try
{
count1[0] = inStream.readLine(array1, 0, array1.length);
if(count1[0] == -1)
return null;
}
catch(IOException e)
{
e.printStackTrace();
}
if(count1[0] == -1)
return null;
if(coding == null)
return new String(array1, 0, count1[0]);
else
return new String(array1, 0, count1[0], coding);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -