各位高手,我用java写了个ftp上传的例子,是利用分析web页面提交过来的数据流
然后将文件部分分离,写到byte变量里,然后用TelnetOutputStream 的 write的 方法写到服务器上。后来我改了方法,循环将byte变量写到TelnetOutputStream 里,每次只写1kb这样才解决了文件上传的问题但是现在出现了新问题,就是文件上传到ftp服务器上以后文件格式就被破坏了,一打开就提示文件出错需要修复,请问
各位高手这是什么原因
以下是往ftp写byte的方法 public void upFile(byte[] sourceData, String destination) throws Exception {
buildList(destination.substring(0, destination.lastIndexOf("/")));
ftpclient.binary(); //此行代码必须放在buildList之后 TelnetOutputStream ftpOut = ftpclient.put(destination);
/*
System.out.println(sourceData.length);
java.io.ByteArrayInputStream oByteSource=new java.io.ByteArrayInputStream(sourceData);
TelnetInputStream ftpIn = new TelnetInputStream(oByteSource, true);
byte[] buf = new byte[1024];
int bufsize = 0;
while((bufsize = ftpIn.read(buf, 0, buf.length)) != -1) {
ftpOut.write(buf, 0, bufsize);
}
*/
int bufsize=1000;
byte[] buf=new byte[bufsize];
int beginpos=0;
int endpos=0;
//每次按buffsize写字节,只到写满为止
do{
if(endpos+bufsize>sourceData.length){
endpos+=(sourceData.length%bufsize);
bufsize=sourceData.length%bufsize;
System.out.println("sourceData.length:"+sourceData.length+"beginpos:"+beginpos+";endpos:"+endpos);
}else{
endpos+=bufsize;
}
for(int i=0;i<bufsize;i++){
buf[i]=sourceData[beginpos+i];
} ftpOut.write(buf);
beginpos+=bufsize;
}while(endpos<sourceData.length); ftpOut.flush();
ftpOut.close();
}
解决方案 »
- web 技术
- JAVA程序实现上传的两张excel表内容对比判断
- tomcat中工程部署不成功
- 怎么生成动态树
- jxl Excel 导入的问题
- 如何获得<input type="file">的值
- 如何获得ResultSet记录集中记录的条数?
- 请问一个关于checkbox的问题????
- public class ClickMe extends Applet implements MouseListener
- 如何将数据从A库存入B库A中存入B的状态发生变化
- jsp的管理框架页面怎么做啊?能不能给段源码?我都快急死了
- 关于JSP中使用IF语句的错误,为了实现读取空数据后报告一句话,急!~!~!~!~!~!~急急!~!~!~!~!~!
//主要是完成request输入流中的字段进行分析,form中加enctype="multipart/form-data"
int totalRead = 0;
int readBytes = 0;
long totalFileSize = 0L;
boolean found = false;
String dataHeader = new String();
String fieldName = new String();
String fileName = new String();
String fileExt = new String();
String filePathName = new String();
String contentType = new String();
String contentDisp = new String();
String typeMIME = new String();
String subTypeMIME = new String();
boolean isFile = false;
m_totalBytes = m_request.getContentLength();
m_binArray = new byte[m_totalBytes];
for (; totalRead < m_totalBytes; totalRead += readBytes) {
try {
m_request.getInputStream();
readBytes = m_request.getInputStream().read(m_binArray,
totalRead, m_totalBytes - totalRead);
} catch (Exception e) {
e.printStackTrace();
}
}
//////////////////////////输出request的流内容
// String temp = new String(m_binArray);
// System.out.println(temp); for (; !found && m_currentIndex < m_totalBytes; m_currentIndex++) {
if (m_binArray[m_currentIndex] == 13) {
System.out.println("Find Data!!");
found = true;
} else {
m_boundary = m_boundary + (char) m_binArray[m_currentIndex];
}
} if (m_currentIndex == 1) {
return;
}
m_currentIndex++;
do {
if (m_currentIndex >= m_totalBytes) {
break;
}
dataHeader = getDataHeader();
m_currentIndex = m_currentIndex + 2;
isFile = dataHeader.indexOf("filename") > 0;
fieldName = getDataFieldValue(dataHeader, "name");
if (isFile) {
filePathName = getDataFieldValue(dataHeader, "filename"); //C:\Documents and Settings\Administrator\My Documents\sun.net.ftp.txt
fileName = getFileName(filePathName); //sun.net.ftp.txt
fileExt = getFileExt(fileName); //txt
contentType = getContentType(dataHeader); // text/plain
contentDisp = getContentDisp(dataHeader); // form-data
typeMIME = getTypeMIME(contentType); //text
subTypeMIME = getSubTypeMIME(contentType); //plain
}
///////////////////
//取指FORM中字段的内容,原理就是定位m_startData,m_endData
int searchPos = m_currentIndex;
int keyPos = 0;
int boundaryLen = m_boundary.length();
m_startData = m_currentIndex;
m_endData = 0;
do {
if (searchPos >= m_totalBytes) {
break;
}
if (m_binArray[searchPos] == (byte) m_boundary.charAt(keyPos)) {
if (keyPos == boundaryLen - 1) {
m_endData = ((searchPos - boundaryLen) + 1) - 3;
break;
}
searchPos++;
keyPos++;
} else {
searchPos++;
keyPos = 0;
}
} while (true);
m_currentIndex = m_endData + boundaryLen + 3;////////////////// if (isFile && fileName.length() > 0) { //对文件的处理,并且必须是不为空的文件域
totalFileSize += (m_endData - m_startData) + 1;//////////////使用相关的代码 int countbyte = (m_endData - m_startData) + 1;
byte[] tempData = new byte[countbyte];
for (int i = 0; i < countbyte; i++) {
tempData[i] = m_binArray[m_startData + i];
}
//将文件数据赋值给内部 HashMap 变量
m_byteFileFieldMap.put(fieldName,tempData); /*
FtpUpfile fupfile = new FtpUpfile(m_ftpserver,m_ftpport,m_ftploginid,m_ftploginpass);
fupfile.login();
// fupfile.upFile(tempData, path + fileName);
fupfile.upFile(tempData, "/" + fileName);
fupfile.logout(); */
//生成文件完全正确
/*
java.io.FileOutputStream fout = new java.io.FileOutputStream(
"c:\\" + fileName);
fout.write(m_binArray, m_startData,
(m_endData - m_startData) + 1);
fout.close();
*/ //输出文件字段中的文件内容
/* System.out.println("*******文件" + fileName + "开始输出");
System.out.println(new String(m_binArray, m_startData,
(m_endData - m_startData) + 1));
System.out.println("*******文件" + fileName + "输出完毕\n");
*/
} if (isFile) { //如果上传域是文件域则进行的一些处理代码
//可以将文件名和文件内容存放在HashMap中
//文件域名称、文件名保存在HashMap中以便JSP中能得到FORM所有的字段名和值
putParameter(fieldName, fileName);
} else {
//可以将非文件域保存在java.util.HashMap
String value = new String(m_binArray, m_startData,
(m_endData - m_startData) + 1);
// System.out.println(fieldName + ":" + value);
putParameter(fieldName, value); //保存在HashMap中
} if ((char) m_binArray[m_currentIndex + 1] == '-') {
break;
}
m_currentIndex = m_currentIndex + 2;
} while (true);
}