Maison > développement back-end > tutoriel php > 错误集-使用apache-nginx搭建图片服务器 完成图片上传功能

错误集-使用apache-nginx搭建图片服务器 完成图片上传功能

WBOY
Libérer: 2016-07-29 09:03:14
original
1424 Les gens l'ont consulté

利用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>
Copier après la connexion

- 坑爹错误

  • 错误描述
    • 使用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>
Copier après la connexion
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

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

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal