通过URL类, 从FTP上读取文件,并将他们写入本地时发现
1 :execl有时无法打开
2 : jpg文件大部分是和原来的有色差
代码如下:
package ftp;import java.net.*;
import java.io.*;public class downLoad { URL url; InputStream is; BufferedInputStream dis; public BufferedInputStream getstream(String file) {
try {
url = new URL("ftp", "192.168.0.253", 21, file);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
is = url.openStream();
dis = new BufferedInputStream(is);
} catch (IOException ee) {
ee.printStackTrace();
} return dis;
} public static void main(String agrs[]) { File file = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
byte[] buff = new byte[2048];
downLoad ftp = new downLoad();
String filepath[] = { "/lwb/pdf.pdf", "/lwb/word.doc", "/lwb/xls.xls",
"/lwb/jpg.jpg" };
String localfilepath[] = { "c:\\temp.pdf", "c:\\temp.doc",
"c:\\temp.xls", "c:\\temp.jpg" };
for (int i = 0; i < 4; i++) {
try {
file = new File(localfilepath[i]);
bos = new BufferedOutputStream(new FileOutputStream(file));
bis = ftp.getstream(filepath[i]);
while ((bis.read(buff, 0, buff.length)) != -1) {
bos.write(buff, 0, buff.length);
}
if (bis != null)
bis.close();
if (bos != null)
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}请大家帮帮忙
1 :execl有时无法打开
2 : jpg文件大部分是和原来的有色差
代码如下:
package ftp;import java.net.*;
import java.io.*;public class downLoad { URL url; InputStream is; BufferedInputStream dis; public BufferedInputStream getstream(String file) {
try {
url = new URL("ftp", "192.168.0.253", 21, file);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
is = url.openStream();
dis = new BufferedInputStream(is);
} catch (IOException ee) {
ee.printStackTrace();
} return dis;
} public static void main(String agrs[]) { File file = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
byte[] buff = new byte[2048];
downLoad ftp = new downLoad();
String filepath[] = { "/lwb/pdf.pdf", "/lwb/word.doc", "/lwb/xls.xls",
"/lwb/jpg.jpg" };
String localfilepath[] = { "c:\\temp.pdf", "c:\\temp.doc",
"c:\\temp.xls", "c:\\temp.jpg" };
for (int i = 0; i < 4; i++) {
try {
file = new File(localfilepath[i]);
bos = new BufferedOutputStream(new FileOutputStream(file));
bis = ftp.getstream(filepath[i]);
while ((bis.read(buff, 0, buff.length)) != -1) {
bos.write(buff, 0, buff.length);
}
if (bis != null)
bis.close();
if (bos != null)
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}请大家帮帮忙
jpg有时候和原来的一样,有的时候和原来的有色差,图片也有些变形.
B机器环境:windows xp sp2,iis5,配了ftp
情况一:如果文档(word,excel,pdf,jpg)等资料放在 运行着tomcat的机器上,通过文件流的方式response, 在A机器和B机器上浏览这些文档正常
情况二:如果文档放在另外一个ftp服务器上.通过JDK的URL类得到文件流response给客户端
此时出现了问题
对比情况一和情况二,
我推测问题是发生在 www的机器从ftp服务器读这些文档上
于是有了我开始的提问.
由于我这里采用了缓冲字节流, 在write()之后没有 flush().后来我加了这一句。
分两种情况测试:downLoad.java
1 重复了10次 word,pdf,excel全部正常了(excel也正常了不知道是不是加上了flush()的原 因)jpg则全部不正常了
2 将downLoad.java中的for()循环去除.单独测试word,excel,pdf,jpg 重复了10次,没有问题还有一个现象:来自于情况二中的测试结果
在A机器上测试只jpg文件的色差.变形 几率小(word,excel.pdf正常)
在B机器上测试.jpg文件的色差.变形 几率特别大;pdf文件信息有时读不完整。(B机器上没有word和excel结果不知道)不知道我有没说清楚.
请大家帮帮忙,帮我分析分析.
这个问题解决不了,我就卡在这里了!
通过链接形如:<a href="ftp://192.168.0.253/lwb/jpg.jpg">test</a>.是没有问题的,网络应该不会有什么问题吧。下面这个jsp文件是将jpg文件从ftp上以字节流读出并写入response中.同时将这个字节流写入到www服务器的某个文件中。这样做只是用来检查是否从ftp上读文件时就产生错误。
(上面那个downLoad.java在测试单个文件时,能正确从ftp上读出文件;通过for循环则会使某些文件信息读取不完整)
<%@page language="java" pageEncoding="gb2312" import="java.io.*"%>
<%@page import="java.net.*"%>
<%
URL url = null;
InputStream is = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
URLConnection urlconn = null;
byte[] buff = new byte[2048];
int bytesRead = 0;
int size = 0;
response.setContentType("image/jpeg");
response.setHeader("Content-disposition", "file=xx"); try {
url = new URL("ftp", "192.168.0.253", 21, "/lwb/jpg.jpg");
urlconn = url.openConnection();
size = urlconn.getContentLength();
response.setContentLength(size);
bis = new BufferedInputStream(urlconn.getInputStream());
bos=new BufferedOutputStream(new FileOutputStream("c:\\temp.jpg"));
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
response.getOutputStream().write(buff, 0, buff.length);
response.getOutputStream().flush();//写浏览器
bos.write(buff,0,buff.length);
bos.flush();//写文件
} } catch (IOException ee) {
ee.printStackTrace();
} finally {
bis.close();
is.close();
bos.close();
}
%>由于写文件比较慢,我测试了几次.均正常;
当我把写文件这一步撤掉后,问题又来了,jpg图象色差,错位(用变形不正确)这个究竟是怎么回事啊?我还想问下,通过URL将FTP上的文件copy下来,为什么copy单个文件没有问题
用for循环copy多个文件就出问题了(信息读取不完整)
这些文件中最大的才200K不到
相关查询请见
http://bugs.sun.com/bugdatabase/