当前位置:我的异常网» Java Web开发 » 请问,原本在windows下正常上传文件但在Linux系统下
请问,原本在windows下正常上传文件但在Linux系统下就不行
www.myexceptions.net 网友分享于:2015-08-26 浏览:172次
请教,原本在windows下正常上传文件但在Linux系统下就不行
upFile//定义上载文件的最大字节
int MAX_SIZE = 102400 * 102400;
// 创建根路径的保存变量
String rootPath;
//声明文件读入类
DataInputStream in = null;
FileOutputStream fileOut = null;
//取得客户端的网络地址
String remoteAddr = request.getRemoteAddr();
//获得服务器的名字
String serverName = request.getServerName();
out.print( "serverName= "+serverName+ "
");
//取得jsp文件相对与根地址的地址
out.print( "request.getServletPath()= "+request.getServletPath()+ "
");
//取得互联网程序的绝对地址
String realPath = request.getRealPath(serverName);
out.println( "request.getRealPath(serverName)= "+realPath+ "
");
realPath = realPath.substring(0,realPath.lastIndexOf( "\\ "));
out.print( "realPath= "+realPath+ "
");
//创建文件的保存目录
//rootPath = realPath + "\\upload\\ ";
rootPath = realPath + "\\upload\\ " + request.getParameter( "folderName ") + "\\ ";
out.println( "文件的保存目录= "+rootPath);
//取得客户端上传的数据类型
String contentType = request.getContentType();
out.println( "
客户端上传的数据类型 = " + contentType + "
");try{
if(contentType.indexOf( "multipart/form-data ") > = 0){
//读入上传的数据
in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
if(formDataLength > MAX_SIZE){
out.println( "
上传的文件字节数不可以超过 " + MAX_SIZE + "
");return;
}
//保存上传文件的数据
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
//上传的数据保存在byte数组
while(totalBytesRead < formDataLength){
byteRead = in.read(dataBytes,totalBytesRead,formDataLength);
totalBytesRead += byteRead;
}
//根据byte数组创建字符串
String file = new String(dataBytes);
//out.println(file);
//取得上传的数据的文件名
String saveFile = file.substring(file.indexOf( "filename=\ " ") + 10);
saveFile = saveFile.substring(0,saveFile.indexOf( "\n "));
saveFile = saveFile.substring(saveFile.lastIndexOf( "\\ ") + 1,saveFile.indexOf( "\ " "));
int lastIndex = contentType.lastIndexOf( "= ");
//取得数据的分隔字符串
String boundary = contentType.substring(lastIndex + 1,contentType.length());
//创建保存路径的文件名
String fileName = rootPath + saveFile;
//String fileName = rootPath + request.getParameter( "fileName ");
文章评论