求帮忙修改上传代码(在linux系统下)

快跑蜗牛哥 2010-05-28 07:26:26
在xp、2003系统下能正常运行,在linux系统下出错
具体:错误原因
endPos = ((file.substring(0,boundaryLocation)).getBytes()).length;  

获得endPos的值,在linux系统下与其他系统下不一致
代码如下

<%@ page contentType="text/html; charset=UTF-8" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%>
<html>
<head>
<title> upFile </title>
</head>
<body bgcolor="#ffffff">
<center>
<%
String rootPath;
DataInputStream in = null;
FileOutputStream fileOut = null;
String realPath = request.getRealPath("/");
rootPath = realPath + "/upload/";
String contentType = request.getContentType();
try{
if(contentType.indexOf("multipart/form-data") >= 0){
in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while(totalBytesRead < formDataLength){
byteRead = in.read(dataBytes,totalBytesRead,formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
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;
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n",pos) + 1;
pos = file.indexOf("\n",pos) + 1;
pos = file.indexOf("\n",pos) + 1;
int boundaryLocation = file.indexOf(boundary,pos) - 4;
int startPos = ((file.substring(0,pos)).getBytes()).length;
int endPos = ((file.substring(0,boundaryLocation)).getBytes()).length;
File checkFile = new File(fileName);
if(checkFile.exists()){
return;
}
File fileDir = new File(rootPath);
if(!fileDir.exists()){
fileDir.mkdirs();
}
fileOut = new FileOutputStream(fileName);
fileOut.write(dataBytes,startPos,(endPos - startPos));
fileOut.close();
}else{
String content = request.getContentType();
}
}catch(Exception ex){
throw new ServletException(ex.getMessage());
}
%>
</center>
</body>
</html>


...全文
100 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
bayougeng 2010-06-01
  • 打赏
  • 举报
回复
你如何确定不是boundaryLocation这个变量的值出错了呢?
若鱼1919 2010-06-01
  • 打赏
  • 举报
回复
你在windows下换个压缩文件试试
逆风向前进 2010-05-31
  • 打赏
  • 举报
回复
原来如此,谢谢
stl0 2010-05-31
  • 打赏
  • 举报
回复
看楼住代码,发现有两个问题

首先,文件夹分隔符separator

(Windows)
C:\Documents and Settings\test\test.txt

(Linux)
/home/users/test/test.txt

其次,换行符
Windows … CRLF \r\n
Linux … LF \n

若鱼1919 2010-05-31
  • 打赏
  • 举报
回复
String file = new String(dataBytes);这个也是按照默认的编码方式把字节数组转化成字符串,问题是你上传的是图片,能转化成字符串吗?

并且,你是把上传的内容一次性的放到一个字节数组里面,万一是上传大文件,极容易就内存溢出了

解析request的body不是那么简单的事情,可以参考一下apache-commons里面那个文件上传组件的源代码。

izard999 2010-05-31
  • 打赏
  • 举报
回复
学习下,, linux不太熟!
快跑蜗牛哥 2010-05-31
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 xuexijava 的回复:]
学习帮顶。。。。。。。。。。。
[/Quote]
帮帮忙啊,
上传后的图片不能打开了!!
快跑蜗牛哥 2010-05-31
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 goldenfish1919 的回复:]
linux的默认编码方式可能与windows的不一样
endPos = ((file.substring(0,boundaryLocation)).getBytes()).length;
改为:
endPos = ((file.substring(0,boundaryLocation)).getBytes("GBK")).length;
试试
[/Quote]
试过,还是一样啊!!
快跑蜗牛哥 2010-05-31
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 goldenfish1919 的回复:]
String file = new String(dataBytes);这个也是按照默认的编码方式把字节数组转化成字符串,问题是你上传的是图片,能转化成字符串吗?

并且,你是把上传的内容一次性的放到一个字节数组里面,万一是上传大文件,极容易就内存溢出了

解析request的body不是那么简单的事情,可以参考一下apache-commons里面那个文件上传组件的源代码。
[/Quote]
//在xp/2003系统下可以显示啊,
但在linux系统下图片不能打开!!
若鱼1919 2010-05-29
  • 打赏
  • 举报
回复
linux的默认编码方式可能与windows的不一样
endPos = ((file.substring(0,boundaryLocation)).getBytes()).length;
改为:
endPos = ((file.substring(0,boundaryLocation)).getBytes("GBK")).length;
试试

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧