java写的ftp服务器有问题?关于文件流

feifeimy_2008 2009-06-30 10:06:02
以下代码是服务器端的一部分。很疑惑的是,服务器可以实现正常的下载,但是,客户端上传的文件,文本文件能够打开,WORD,音乐,照片虽然上传成功了,但是打不开,有问题。但是,不论什么文件,下载的都是正常的,都可以正常打开。为什么?
我分析了一下代码,感觉用的流是二进行的输入输出流,为什么下载的时候什么文件都可以下载成功了?希望高手帮忙!!!!

private void sendFile() throws IOException {
int BUFFER_SIZE = 1024*10;
try {
Socket socket;
System.out.println("读取客户端的端口和IP地址...");
System.out.println(request.getIP());
System.out.println(request.getPort());
socket = new Socket(request.getIP(), request.getPort());
System.out.println("读取成功.....");
OutputStream output = socket.getOutputStream();

byte[] bytes = new byte[BUFFER_SIZE];
FileInputStream fis = null;
try {
File file = new File(workingDir.getPath(), request.getFilename());
System.out.println("下载的文件名是:"+request.getFilename());
System.out.println("目录是:"+workingDir.getPath());
if (file.exists()) {
fis = new FileInputStream(file);
int ch = fis.read(bytes, 0, BUFFER_SIZE);
while (ch!=-1) {
output.write(bytes, 0, ch);
output.flush();
ch = fis.read(bytes, 0, BUFFER_SIZE);
}
sendMsg("226 File sent ok.");
}
else {
// file not found
sendMsg("550 cannot open File: " + request.getFilename() + " : No Such File.");
}

}
catch (Exception e) {
// thrown if cannot instantiate a File object
System.out.println(e.toString() );
System.out.println("sendFile()错误!在response里面");
}
finally {
if (fis!=null)
fis.close();
}
socket.close();
}catch(Exception e)
{
System.out.println(e.toString() );
}
}

//receive file to current working directory
private void receiveFile() throws IOException {
int BUFFER_SIZE = 1024*10;
try {
Socket socket;
socket = new Socket(request.getIP(), request.getPort());

InputStream input = socket.getInputStream();

byte[] bytes = new byte[BUFFER_SIZE];
FileOutputStream fos = null;
try {
File file = new File(workingDir.getPath(), request.getFilename());
if (file.createNewFile()) {
fos = new FileOutputStream(file);
int ch = input.read(bytes, 0, BUFFER_SIZE);
while (ch!=-1) {
fos.write(bytes, 0, ch);
fos.flush();
ch = input.read(bytes);
}
sendMsg("226 File received ok.");
}
else {
// file can't write
sendMsg("550 cannot upload File:" + request.getFilename());
}
}
catch (Exception e) {
// thrown if cannot instantiate a File object
System.out.println(e.toString() );
}
finally {
if (fos!=null)
fos.close();
input.close();
}
socket.close();
}catch(Exception e)
{
System.out.println(e.toString() );
}
}
...全文
339 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
jinxfei 2009-06-30
  • 打赏
  • 举报
回复
ftp上传有两种模式,二进制(bin)和文本(ascii)模式,
你说的图片打不开,很可能是你没有设置适当的模式。



你看一看主流的ftp客户端,都有配置哪些扩展名文件作为文本传输。
jinxfei 2009-06-30
  • 打赏
  • 举报
回复
搂主不要做懒人,我已经提供了很关键的信息,希望你自己做研究。

楼上说的也是对的。

henry_fuzr 2009-06-30
  • 打赏
  • 举报
回复
2.视频文件无法正常播放。

问题描述:视频文件上传到ftp服务器上后无法正常播放;

解决方法:在传送视频文件时,使用二进制模式进行,上传时设置binaryTransfer = true;

问题原因:视频文件是二进制文件,而上传的默认方式是ascii方式;

zhanghua4109 2009-06-30
  • 打赏
  • 举报
回复
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import nc.itf.doc.DocDelegator;
import nc.vo.doc.doc_007.DirVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.SuperVO;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTP;


public class FtpTool {


private FTPClient ftp;

private String romateDir = "";

private String userName = "";

private String password = "";

private String host = "";

private String port = "21";

public FtpTool(String url) throws IOException {
//String url="ftp://user:password@ip:port/ftptest/psd";
int len = url.indexOf("//");
String strTemp = url.substring(len + 2);
len = strTemp.indexOf(":");
userName = strTemp.substring(0, len);
strTemp = strTemp.substring(len + 1);

len = strTemp.indexOf("@");
password = strTemp.substring(0, len);
strTemp = strTemp.substring(len + 1);
host = "";
len = strTemp.indexOf(":");
if (len < 0)//没有设置端口
{
port = "21";
len = strTemp.indexOf("/");
if (len > -1) {
host = strTemp.substring(0, len);
strTemp = strTemp.substring(len + 1);
} else {
strTemp = "";
}
} else {
host = strTemp.substring(0, len);
strTemp = strTemp.substring(len + 1);
len = strTemp.indexOf("/");
if (len > -1) {
port = strTemp.substring(0, len);
strTemp = strTemp.substring(len + 1);
} else {
port = "21";
strTemp = "";
}
}
romateDir = strTemp;
ftp = new FTPClient();
ftp.connect(host, FormatStringToInt(port));

}

public FtpTool(String host, int port) throws IOException {
ftp = new FTPClient();
ftp.connect(host, port);
}

public String login(String username, String password) throws IOException {
this.ftp.login(username, password);
return this.ftp.getReplyString();
}

public String login() throws IOException {
this.ftp.login(userName, password);
System.out.println("ftp用户: " + userName);
System.out.println("ftp密码: " + password);
if (!romateDir.equals(""))
System.out.println("cd " + romateDir);
ftp.changeWorkingDirectory(romateDir);
return this.ftp.getReplyString();
}

public boolean upload(String pathname, String filename) throws IOException, BusinessException {

int reply;
int j;
String m_sfilename = null;
filename = CheckNullString(filename);
if (filename.equals(""))
return false;
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.out.println("FTP server refused connection.");
System.exit(1);
}
FileInputStream is = null;
try {
File file_in;
if (pathname.endsWith(File.separator)) {
file_in = new File(pathname + filename);
} else {
file_in = new File(pathname + File.separator + filename);
}
if (file_in.length() == 0) {
System.out.println("上传文件为空!");
return false;
}
//产生随机数最大到99
j = (int)(Math.random()*100);
m_sfilename = String.valueOf(j) + ".pdf"; // 生成的文件名
is = new FileInputStream(file_in);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.storeFile(m_sfilename, is);
ftp.logout();


} finally {
if (is != null) {
is.close();
}
}
System.out.println("上传文件成功!");
return true;
}


public boolean delete(String filename) throws IOException {

FileInputStream is = null;
boolean retValue = false;
try {
retValue = ftp.deleteFile(filename);
ftp.logout();
} finally {
if (is != null) {
is.close();
}
}
return retValue;

}

public void close() {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public static int FormatStringToInt(String p_String) {
int intRe = 0;
if (p_String != null) {
if (!p_String.trim().equals("")) {
try {
intRe = Integer.parseInt(p_String);
} catch (Exception ex) {

}
}
}
return intRe;
}

public static String CheckNullString(String p_String) {
if (p_String == null)
return "";
else
return p_String;
}

public boolean downfile(String pathname, String filename) {

String outputFileName = null;
boolean retValue = false;
try {
FTPFile files[] = ftp.listFiles();
int reply = ftp.getReplyCode();

////////////////////////////////////////////////
if (!FTPReply.isPositiveCompletion(reply)) {
try {
throw new Exception("Unable to get list of files to dowload.");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/////////////////////////////////////////////////////
if (files.length == 0) {
System.out.println("No files are available for download.");
}else {
for (int i=0; i <files.length; i++) {
System.out.println("Downloading file "+files[i].getName()+"Size:"+files[i].getSize());
outputFileName = pathname + filename + ".pdf";
//return outputFileName;
File f = new File(outputFileName);

//////////////////////////////////////////////////////
retValue = ftp.retrieveFile(outputFileName, new FileOutputStream(f));

if (!retValue) {
try {
throw new Exception ("Downloading of remote file"+files[i].getName()+" failed. ftp.retrieveFile() returned false.");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
}

/////////////////////////////////////////////////////////////
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return retValue;
}

}
试试这个
feifeimy_2008 2009-06-30
  • 打赏
  • 举报
回复
用的就是jdk的ftpclient来实现的,所以现在上传不成功,怀疑是服务器有问题!
lz12366007 2009-06-30
  • 打赏
  • 举报
回复
还在学习中~~~~好像得有个识别文件头的过程吧
linyu1247 2009-06-30
  • 打赏
  • 举报
回复
jdk好像有自带的FTPClient
feifeimy_2008 2009-06-30
  • 打赏
  • 举报
回复
还有补充一下,下载正常,上传的文件都有问题,除了文本文件外。
feifeimy_2008 2009-06-30
  • 打赏
  • 举报
回复
能不能说的再具体一点了!您的意思是服务器端没有问题么?可以这样去写服务器端么?

62,621

社区成员

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

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