search
HomeBackend DevelopmentPHP TutorialError set - Use apache-nginx to build an image server to complete the image upload function

利用apache-nginx服务来搭建图片服务器
使用资源:
vmware+ centos + nginx +vsftpd
nginx :存储图片的服务。 详见-nginx 安装手册
vsftpd :图片上传的传输协议。 详见-vsftpd安装手册

使用jar包:
org.apache.commons.net
封装工具类:
FtpUtil

开发代码:

<code><span>public</span><span><span>class</span><span>FTPTest</span> {</span><span>@Test</span><span>public</span><span>void</span><span>testFtpClient</span>() <span>throws</span> Exception {
        <span>// 创建一个FTPClient对象</span>
        FTPClient ftpClient = <span>new</span> FTPClient();
        <span>// 创建ftp连接 ftp默认端口号是21</span>
        ftpClient.connect(<span>"192.168.137.128"</span>, <span>21</span>);
        <span>// 登录FTP服务器,使用用户名、密码</span>
        ftpClient.login(<span>"ftpuser"</span>, <span>"ftpuser"</span>);
        <span>// 上传文件</span><span>// 读取本地文件</span>
        InputStream inputStream = <span>new</span> FileInputStream(<span>new</span> File(<span>"C:\\Users\\Public\\Pictures\\Sample Pictures\\2.jpg"</span>));
        <span>// 设置上传路径</span>
        ftpClient.changeWorkingDirectory(<span>"/home/ftpuser/www/images"</span>);
        <span>// 修改上传文件的格式</span>
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

        <span>// 参数一 服务器文件名</span><span>// 参数二 上传文件的inputStream</span>
        ftpClient.storeFile(<span>"hello1.jpg"</span>, inputStream);
        <span>// 关闭连接</span>
        inputStream.close();
        ftpClient.logout();
    }

    <span>// host FTP服务器hostname</span><span>// port FTP服务器端口</span><span>// username FTP登录账号</span><span>// password FTP登录密码</span><span>// basePath FTP服务器基础目录</span><span>// filePath FTP服务器文件存放路径。例如分日期存放:/2015/01/01。文件的路径为basePath+filePath</span><span>// filename 上传到FTP服务器上的文件名</span><span>// input 输入流</span><span>@Test</span><span>public</span><span>void</span><span>testFtpUtils</span>() <span>throws</span> FileNotFoundException {
        InputStream inputStream = <span>new</span> FileInputStream(<span>new</span> File(<span>"C:\\Users\\Public\\Pictures\\Sample Pictures\\2.jpg"</span>));
        <span>boolean</span> a = FtpUtil.uploadFile(<span>"192.168.137.128"</span>, <span>21</span>, <span>"ftpuser"</span>, <span>"ftpuser"</span>, <span>"/home/ftpuser/www/images"</span>,
                <span>"/2016/02/17"</span>, <span>"test.jpg"</span>, inputStream);
        System.out.println(a);
    }


    <span>@Test</span><span>public</span><span>void</span><span>writeFile</span>() <span>throws</span> FileNotFoundException {
        String newFileName = <span>null</span>;<span>// 上传到服务器上文件名称</span>
        FTPClient ftp = <span>null</span>;
        InputStream inputStream = <span>new</span> FileInputStream(<span>new</span> File(<span>"C:\\Users\\Public\\Pictures\\Sample Pictures\\2.jpg"</span>));
        <span>try</span> {
            ftp = <span>new</span> FTPClient();
            ftp.connect(<span>"192.168.137.128"</span>, <span>21</span>);<span>// 建立连接</span><span>boolean</span> b = ftp.login(<span>"ftpuser"</span>, <span>"ftpuser"</span>);<span>// 登陆</span><span>if</span> (!b)
                System.out.println(<span>"failed"</span>);
            <span>int</span> reply = ftp.getReplyCode();
            <span>if</span> (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                System.out.println(<span>"failed"</span>);
            }
            <span>/**
             * 设置上传类型,图片是二进制图片 默认使用的是ASCII编码的。因为图片是二进制文件。所以要设置一下 否则上传的图片就是大花脸了
             */</span><span>boolean</span> result = ftp.setFileType(ftp.BINARY_FILE_TYPE);
            ftp.changeWorkingDirectory(<span>"/home/ftpuser/www/images"</span>);

            String fileName = <span>"/hello1.jpg"</span>;
            <span>boolean</span> bb = ftp.storeFile(fileName, inputStream);<span>// 指定到服务器的文件名</span>
        } <span>catch</span> (IOException e) {
            System.out.println(<span>"连接ftp失败!"</span>);
            System.out.println(<span>"failed"</span>);
        } <span>finally</span> {
            <span>try</span> {
                <span>if</span> (inputStream != <span>null</span>)
                    inputStream.close();
                <span>if</span> (ftp != <span>null</span> || ftp.isConnected())
                    ftp.logout();
            } <span>catch</span> (IOException e1) {
            }
        }
        System.out.println(<span>"上传文件成功!"</span>);
        System.out.println(newFileName);<span>// 返回上传文件的文件名</span>
    }
}</code>

- 坑爹错误

  • 错误描述
    • 使用Xshell 和 Xftp 连接linux没有问题,工具传输文件也没有问题,唯独使用代码完成上传时报错。且linux服务器有时会生成对应的文件夹路径,但其中并没有文件。
    • 使用了Vmware运行nginx和vsftpd,开始怀疑是linux端口或者linux的防火墙没开 造成的传输失败 就开了2个 linux虚拟机。结果error依旧。随后又重新安装了nginx和vsftpd服务。error依旧。
  • 错误原因:

  • 本机windows的防火墙没关!!! 坑爹吧……
  • 控制太报错如下:
<code><span>java.net.SocketException:</span> Software caused connection abort: recv failed
    at java<span>.net</span><span>.SocketInputStream</span><span>.socketRead</span>0(Native Method)
    at java<span>.net</span><span>.SocketInputStream</span><span>.read</span>(SocketInputStream<span>.java</span>:<span>152</span>)
    at java<span>.net</span><span>.SocketInputStream</span><span>.read</span>(SocketInputStream<span>.java</span>:<span>122</span>)
    at sun<span>.nio</span><span>.cs</span><span>.StreamDecoder</span><span>.readBytes</span>(StreamDecoder<span>.java</span>:<span>283</span>)
    at sun<span>.nio</span><span>.cs</span><span>.StreamDecoder</span><span>.implRead</span>(StreamDecoder<span>.java</span>:<span>325</span>)
    at sun<span>.nio</span><span>.cs</span><span>.StreamDecoder</span><span>.read</span>(StreamDecoder<span>.java</span>:<span>177</span>)
    at java<span>.io</span><span>.InputStreamReader</span><span>.read</span>(InputStreamReader<span>.java</span>:<span>184</span>)
    at java<span>.io</span><span>.BufferedReader</span><span>.fill</span>(BufferedReader<span>.java</span>:<span>154</span>)
    at java<span>.io</span><span>.BufferedReader</span><span>.read</span>(BufferedReader<span>.java</span>:<span>175</span>)
    at org<span>.apache</span><span>.commons</span><span>.net</span><span>.io</span><span>.CRLFLineReader</span><span>.readLine</span>(CRLFLineReader<span>.java</span>:<span>58</span>)
    at org<span>.apache</span><span>.commons</span><span>.net</span><span>.ftp</span><span>.FTP</span>.__getReply(FTP<span>.java</span>:<span>314</span>)
    at org<span>.apache</span><span>.commons</span><span>.net</span><span>.ftp</span><span>.FTP</span>.__getReply(FTP<span>.java</span>:<span>294</span>)
    at org<span>.apache</span><span>.commons</span><span>.net</span><span>.ftp</span><span>.FTP</span><span>.sendCommand</span>(FTP<span>.java</span>:<span>483</span>)
    at org<span>.apache</span><span>.commons</span><span>.net</span><span>.ftp</span><span>.FTP</span><span>.sendCommand</span>(FTP<span>.java</span>:<span>608</span>)
    at org<span>.apache</span><span>.commons</span><span>.net</span><span>.ftp</span><span>.FTP</span><span>.port</span>(FTP<span>.java</span>:<span>932</span>)
    at org<span>.apache</span><span>.commons</span><span>.net</span><span>.ftp</span><span>.FTPClient</span>._openDataConnection_(FTPClient<span>.java</span>:<span>812</span>)
    at org<span>.apache</span><span>.commons</span><span>.net</span><span>.ftp</span><span>.FTPClient</span>._storeFile(FTPClient<span>.java</span>:<span>633</span>)
    at org<span>.apache</span><span>.commons</span><span>.net</span><span>.ftp</span><span>.FTPClient</span>.__storeFile(FTPClient<span>.java</span>:<span>624</span>)
    at org<span>.apache</span><span>.commons</span><span>.net</span><span>.ftp</span><span>.FTPClient</span><span>.storeFile</span>(FTPClient<span>.java</span>:<span>1976</span>)
    at <span>com</span><span>.taotao</span><span>.utils</span><span>.FtpUtil</span><span>.uploadFile</span>(FtpUtil<span>.java</span>:<span>69</span>)
    at <span>com</span><span>.taotao</span><span>.controller</span><span>.test</span><span>.FTPTest</span><span>.testFtpUtils</span>(FTPTest<span>.java</span>:<span>82</span>)
    at sun<span>.reflect</span><span>.NativeMethodAccessorImpl</span><span>.invoke</span>0(Native Method)
    at sun<span>.reflect</span><span>.NativeMethodAccessorImpl</span><span>.invoke</span>(NativeMethodAccessorImpl<span>.java</span>:<span>57</span>)
    at sun<span>.reflect</span><span>.DelegatingMethodAccessorImpl</span><span>.invoke</span>(DelegatingMethodAccessorImpl<span>.java</span>:<span>43</span>)
    at java<span>.lang</span><span>.reflect</span><span>.Method</span><span>.invoke</span>(Method<span>.java</span>:<span>606</span>)
    at org<span>.junit</span><span>.runners</span><span>.model</span><span>.FrameworkMethod</span>$1<span>.runReflectiveCall</span>(FrameworkMethod<span>.java</span>:<span>50</span>)
    at org<span>.junit</span><span>.internal</span><span>.runners</span><span>.model</span><span>.ReflectiveCallable</span><span>.run</span>(ReflectiveCallable<span>.java</span>:<span>12</span>)
    at org<span>.junit</span><span>.runners</span><span>.model</span><span>.FrameworkMethod</span><span>.invokeExplosively</span>(FrameworkMethod<span>.java</span>:<span>47</span>)
    at org<span>.junit</span><span>.internal</span><span>.runners</span><span>.statements</span><span>.InvokeMethod</span><span>.evaluate</span>(InvokeMethod<span>.java</span>:<span>17</span>)
    at org<span>.junit</span><span>.runners</span><span>.ParentRunner</span><span>.runLeaf</span>(ParentRunner<span>.java</span>:<span>325</span>)
    at org<span>.junit</span><span>.runners</span><span>.BlockJUnit</span>4ClassRunner<span>.runChild</span>(BlockJUnit4ClassRunner<span>.java</span>:<span>78</span>)
    at org<span>.junit</span><span>.runners</span><span>.BlockJUnit</span>4ClassRunner<span>.runChild</span>(BlockJUnit4ClassRunner<span>.java</span>:<span>57</span>)
    at org<span>.junit</span><span>.runners</span><span>.ParentRunner</span>$3<span>.run</span>(ParentRunner<span>.java</span>:<span>290</span>)
    at org<span>.junit</span><span>.runners</span><span>.ParentRunner</span>$1<span>.schedule</span>(ParentRunner<span>.java</span>:<span>71</span>)
    at org<span>.junit</span><span>.runners</span><span>.ParentRunner</span><span>.runChildren</span>(ParentRunner<span>.java</span>:<span>288</span>)
    at org<span>.junit</span><span>.runners</span><span>.ParentRunner</span><span>.access</span>$000(ParentRunner<span>.java</span>:<span>58</span>)
    at org<span>.junit</span><span>.runners</span><span>.ParentRunner</span>$2<span>.evaluate</span>(ParentRunner<span>.java</span>:<span>268</span>)
    at org<span>.junit</span><span>.runners</span><span>.ParentRunner</span><span>.run</span>(ParentRunner<span>.java</span>:<span>363</span>)
    at org<span>.eclipse</span><span>.jdt</span><span>.internal</span><span>.junit</span>4<span>.runner</span><span>.JUnit</span>4TestReference<span>.run</span>(JUnit4TestReference<span>.java</span>:<span>86</span>)
    at org<span>.eclipse</span><span>.jdt</span><span>.internal</span><span>.junit</span><span>.runner</span><span>.TestExecution</span><span>.run</span>(TestExecution<span>.java</span>:<span>38</span>)
    at org<span>.eclipse</span><span>.jdt</span><span>.internal</span><span>.junit</span><span>.runner</span><span>.RemoteTestRunner</span><span>.runTests</span>(RemoteTestRunner<span>.java</span>:<span>459</span>)
    at org<span>.eclipse</span><span>.jdt</span><span>.internal</span><span>.junit</span><span>.runner</span><span>.RemoteTestRunner</span><span>.runTests</span>(RemoteTestRunner<span>.java</span>:<span>675</span>)
    at org<span>.eclipse</span><span>.jdt</span><span>.internal</span><span>.junit</span><span>.runner</span><span>.RemoteTestRunner</span><span>.run</span>(RemoteTestRunner<span>.java</span>:<span>382</span>)
    at org<span>.eclipse</span><span>.jdt</span><span>.internal</span><span>.junit</span><span>.runner</span><span>.RemoteTestRunner</span><span>.main</span>(RemoteTestRunner<span>.java</span>:<span>192</span>)</code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了错误集-使用apache-nginx搭建图片服务器 完成图片上传功能,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.