web.xml
<servlet>
<servlet-name>UpLoadFile</servlet-name>
<display-name>UpLoadFile</display-name>
<servlet-class>nethosp.java.UpLoadFile</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>UpLoadFile</servlet-name>
<url-pattern>/UpLoadFile</url-pattern>
</servlet-mapping>
package nethosp.java;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
public class UpLoadFile
{
public UpLoadFile()
{
uploadDirectory = "uploadfile";
ContentType = "";
CharacterEncoding = "";
}
private String getFileName(String s)
{
int i = s.lastIndexOf("\\");
if(i < 0 || i >= s.length() - 1)
{
i = s.lastIndexOf("/");
if(i < 0 || i >= s.length() - 1)
return s;
}
return s.substring(i + 1);
}
public void setUploadDirectory(String s)
{
uploadDirectory = s;
}
public void setContentType(String s)
{
ContentType = s;
int i;
if((i = ContentType.indexOf("boundary=")) != -1)
{
ContentType = ContentType.substring(i + 9);
ContentType = "--" + ContentType;
}
}
public void setCharacterEncoding(String s)
{
CharacterEncoding = s;
}
public void uploadFile(HttpServletRequest httpservletrequest)
throws ServletException, IOException
{
setCharacterEncoding(httpservletrequest.getCharacterEncoding());
setContentType(httpservletrequest.getContentType());
uploadFile(httpservletrequest.getInputStream());
}
public void uploadFile(ServletInputStream servletinputstream)
throws ServletException, IOException
{
String s = null;
String s1 = null;
byte abyte0[] = new byte[4096];
byte abyte1[] = new byte[4096];
int ai[] = new int[1];
int ai1[] = new int[1];
String s2;
while((s2 = readLine(abyte0, ai, servletinputstream, CharacterEncoding)) != null)
{
int i = s2.indexOf("filename=");
if(i >= 0)
{
s2 = s2.substring(i + 10);
if((i = s2.indexOf("\"")) > 0)
s2 = s2.substring(0, i);
break;
}
}
s1 = s2;
if(s1 != null && !s1.equals("\""))
{
s1 = getFileName(s1);
String s3 = readLine(abyte0, ai, servletinputstream, CharacterEncoding);
if(s3.indexOf("Content-Type") >= 0)
readLine(abyte0, ai, servletinputstream, CharacterEncoding);
File file = new File(uploadDirectory, s1);
FileOutputStream fileoutputstream = new FileOutputStream(file);
while((s3 = readLine(abyte0, ai, servletinputstream, CharacterEncoding)) != null)
{
if(s3.indexOf(ContentType) == 0 && abyte0[0] == 45)
break;
if(s != null)
{
fileoutputstream.write(abyte1, 0, ai1[0]);
fileoutputstream.flush();
}
s = readLine(abyte1, ai1, servletinputstream, CharacterEncoding);
if(s == null || s.indexOf(ContentType) == 0 && abyte1[0] == 45)
break;
fileoutputstream.write(abyte0, 0, ai[0]);
fileoutputstream.flush();
}
byte byte0;
if(newline.length() == 1)
byte0 = 2;
else
byte0 = 1;
if(s != null && abyte1[0] != 45 && ai1[0] > newline.length() * byte0)
fileoutputstream.write(abyte1, 0, ai1[0] - newline.length() * byte0);
if(s3 != null && abyte0[0] != 45 && ai[0] > newline.length() * byte0)
fileoutputstream.write(abyte0, 0, ai[0] - newline.length() * byte0);
fileoutputstream.close();
}
}
private String readLine(byte abyte0[], int ai[], ServletInputStream servletinputstream, String s)
{
try
{
ai[0] = servletinputstream.readLine(abyte0, 0, abyte0.length);
if(ai[0] == -1)
return null;
}
catch(IOException ioexception)
{
return null;
}
try
{
if(s == null)
return new String(abyte0, 0, ai[0]);
else
return new String(abyte0, 0, ai[0], s);
}
catch(Exception exception)
{
return null;
}
}
private static String newline = "\n";
private String uploadDirectory;
private String ContentType;
private String CharacterEncoding;
}
FLASH
_file.upload(new URLRequest("http://localhost:8081/nethosp/UpLoadFile"),"upLoadFile");
我没弄明白这个,大家说说怎么改才对吧