时间:2021-07-01 10:21:17 帮助过:20人阅读
使用jar包:
org.apache.commons.net
封装工具类:
FtpUtil
开发代码:
publicclassFTPTest {@TestpublicvoidtestFtpClient() throws Exception {
// 创建一个FTPClient对象
FTPClient ftpClient = new FTPClient();
// 创建ftp连接 ftp默认端口号是21
ftpClient.connect("192.168.137.128", 21);
// 登录FTP服务器,使用用户名、密码
ftpClient.login("ftpuser", "ftpuser");
// 上传文件// 读取本地文件
InputStream inputStream = new FileInputStream(new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\2.jpg"));
// 设置上传路径
ftpClient.changeWorkingDirectory("/home/ftpuser/www/images");
// 修改上传文件的格式
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// 参数一 服务器文件名// 参数二 上传文件的inputStream
ftpClient.storeFile("hello1.jpg", inputStream);
// 关闭连接
inputStream.close();
ftpClient.logout();
}
// host FTP服务器hostname// port FTP服务器端口// username FTP登录账号// password FTP登录密码// basePath FTP服务器基础目录// filePath FTP服务器文件存放路径。例如分日期存放:/2015/01/01。文件的路径为basePath+filePath// filename 上传到FTP服务器上的文件名// input 输入流@TestpublicvoidtestFtpUtils() throws FileNotFoundException {
InputStream inputStream = new FileInputStream(new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\2.jpg"));
boolean a = FtpUtil.uploadFile("192.168.137.128", 21, "ftpuser", "ftpuser", "/home/ftpuser/www/images",
"/2016/02/17", "test.jpg", inputStream);
System.out.println(a);
}
@TestpublicvoidwriteFile() throws FileNotFoundException {
String newFileName = null;// 上传到服务器上文件名称
FTPClient ftp = null;
InputStream inputStream = new FileInputStream(new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\2.jpg"));
try {
ftp = new FTPClient();
ftp.connect("192.168.137.128", 21);// 建立连接boolean b = ftp.login("ftpuser", "ftpuser");// 登陆if (!b)
System.out.println("failed");
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.out.println("failed");
}
/**
* 设置上传类型,图片是二进制图片 默认使用的是ASCII编码的。因为图片是二进制文件。所以要设置一下 否则上传的图片就是大花脸了
*/boolean result = ftp.setFileType(ftp.BINARY_FILE_TYPE);
ftp.changeWorkingDirectory("/home/ftpuser/www/images");
String fileName = "/hello1.jpg";
boolean bb = ftp.storeFile(fileName, inputStream);// 指定到服务器的文件名
} catch (IOException e) {
System.out.println("连接ftp失败!");
System.out.println("failed");
} finally {
try {
if (inputStream != null)
inputStream.close();
if (ftp != null || ftp.isConnected())
ftp.logout();
} catch (IOException e1) {
}
}
System.out.println("上传文件成功!");
System.out.println(newFileName);// 返回上传文件的文件名
}
}
- 坑爹错误
错误原因:
java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:152)
at java.net.SocketInputStream.read(SocketInputStream.java:122)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:154)
at java.io.BufferedReader.read(BufferedReader.java:175)
at org.apache.commons.net.io.CRLFLineReader.readLine(CRLFLineReader.java:58)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:314)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:294)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:483)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:608)
at org.apache.commons.net.ftp.FTP.port(FTP.java:932)
at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:812)
at org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:633)
at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:624)
at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:1976)
at com.taotao.utils.FtpUtil.uploadFile(FtpUtil.java:69)
at com.taotao.controller.test.FTPTest.testFtpUtils(FTPTest.java:82)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
').addClass('pre-numbering').hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i <= lines; i++) {
$numbering.append($('').text(i));
};
$numbering.fadeIn(1700);
});
});
以上就介绍了错误集-使用apache-nginx搭建图片服务器 完成图片上传功能,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。