E
Emily
Unregistered / Unconfirmed
GUEST, unregistred user!
要求:从客户端上传HTML文件(结尾必须是.html格式) ,放到服务器目录下。以下是代码
代码:
DataInputStream in=null;
FileOutputStream fileOut=null;
String contentType = request.getContentType();
System.out.println("get ContentType:"+contentType);
in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int bytesRead = 0;
String file = new String(dataBytes);
System.out.println("String File:"+file);
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex+1, contentType.length());
System.out.println("lastIndex:"+lastIndex+"String boundary"+boundary);
String directory="";
if (file.indexOf("name=/"Directory/"") > 0){directory = file.substring(file.indexOf("name=/"Directory/""));
directory = directory.substring(directory.indexOf("/n")+1);
directory = directory.substring(directory.indexOf("/n")+1);
directory = directory.substring(0,directory.indexOf("/n")-1);
String saveFile = file.substring( file.indexOf("filename=/"")+10);
System.out.println("saveFile:"+saveFile);
if(saveFile.endsWith(".html"))//如果上载的文件是HTML则可以上传
{
saveFile = saveFile.substring(0,saveFile.indexOf("/n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("//")+1,saveFile.indexOf("/""));
String rootPath="c://";
String fileName = new String(rootPath +directory+saveFile);
System.out.println("fileName:"+fileName);
fileOut = new FileOutputStream(fileName);
//write the string to the file as a byte array
fileOut.write(file.getBytes(),0,file.length());
fileOut.close();
}