解决方案 »
- 自己做的网站怎样推广?
- 想把 list<String> list 对象 序列化 如何显式声明serialVersionUID的值?
- struct2访问List最后一个对象
- hibernate插blob遗留的问题
- 为何引入了org.dom4j.io.SAXReader,但在实例化时报错:java.lang.NoClassDefFoundError: org/dom4j/io/SAXReader?
- 退学?不退?
- JAVA能移植到手机上用吗?
- spring高手请进,急!!!!!!!!!!!!!!!!
- pdf中带图片时,加的图片水印会被遮住,请问有方案解决吗?
- 小弟初学,请教J2EE的环境怎么配?
- 没学历的程序员进来,(高中)
- Lucene检索中文拼音问题
/**
*
* @param url FTP服务器hostname
* @param port FTP服务器端口
* @param username FTP登录账号
* @param password FTP登录密码
* @param path FTP服务器保存目录
* @param filename 上传到FTP服务器上的文件名
* @param input 输入流
* @return 成功返回true,否则返回false
*/
public static boolean uploadFile(String url, int port, String username, String password,
String path, String filename, InputStream input) throws Exception {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);//连接FTP服务器
//如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(username, password);//登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
logger.error("----------->>>连接ftp服务器失败");
//throw new Exception("----------->>>连接ftp服务器失败");
}
logger.info("-----连接ftp服务器成功");
ftp.changeWorkingDirectory(path);
ftp.storeFile(filename, input);
input.close();
ftp.logout();
success = true;
logger.info("----------->>>文件上传成功");
}
catch (Exception e) {
e.printStackTrace();
logger.error("----------->>>ftp上传文件时失败 " + e.getMessage());
//throw new Exception(e);
}
finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
logger.error("----------->>>ftp连接关闭失败 " + ioe.getMessage());
}
}
}
return success;
}断点调试,一行行执行下来,都不报错,就是中文文件传不上去,英文的就能传上去。
你没设置编码
ftpClient.ascii(); 我有这个设置 你加上试试
ftp.setControlEncoding("UTF-8");还是不行
另外找不到 ascii() 这个方法啊
用的是 org.apache.commons.net.ftp.FTPClient
public static FTPClient connectServer(String server, int port) throws SocketException, IOException {
FTPClient ftpClient = new FTPClient(); ftpClient.connect(server, port); ftpClient.enterLocalPassiveMode(); ftpClient.setControlEncoding("GBK"); if(!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
ftpClient.disconnect();
ftpClient = null;
} return ftpClient;
} public static boolean loginFTP(FTPClient ftpClient, String userName, String password) throws IOException {
boolean flag = false; if(ftpClient != null && ftpClient.isConnected()) {
flag = ftpClient.login(userName, password);
ftpClient.setFileType(BINARY_FILE_TYPE);
} return flag;
}public static boolean uploadFile(FTPClient ftpClient, java.io.File localFile, String remoteFileName, Long remoteSize) throws IOException {
boolean flag = false; if(ftpClient != null && ftpClient.isConnected() && localFile.exists()) {
RandomAccessFile raf = new RandomAccessFile(localFile, "r");
OutputStream out = ftpClient.appendFileStream(new String(remoteFileName.getBytes("GBK"), "ISO-8859-1")); if(remoteSize > 0) { //断点上传
ftpClient.setRestartOffset(remoteSize);
raf.seek(remoteSize);
} byte[] bytes = new byte[1024];
int c;
while((c = raf.read(bytes)) != -1) {
out.write(bytes, 0, c);
} out.flush();
raf.close();
out.close(); flag = ftpClient.completePendingCommand();
} return flag;
}这是以前写的FTP方法,你试试看
点睛之笔!就是这句话!