English 中文(简体)
服务器之间的传输图像?
原标题:Transfer images between servers?

我有一个 j瓦服务器,接收图像,供我浏览。

我有另一个服务器(开办的网址),接收所有图像。 我怎么能从我的jsp服务器到我的实验室服务器获得图像? 流动就像:

public class ServletImgUpload extends HttpServlet 
{   
    public void doPost(HttpServletRequest req, HttpServletResponse resp) 
      throws ServletException, IOException 
    {
        // get image user submitted
        // try sending it to my php server now
        // return success or failure message back to user
    }
}

增 编

最佳回答

首先,为什么不要求你直接将表格提交给该政策手册?

<form action="http://example.com/upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit">
</form>

如果这不是一种选择,而且你真的需要将表格提交服务器,那么首先在联合调查组中形成一种超文本形式:

<form action="upload" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit">
</form>

在关于<代码>/upload的的服务器中,根据PHP的文字处理申请有两种选择。

如果PHP的文字采用same参数,并且能够处理上载文件,其方式与超文本格式指示服务器做的相同(我仍然只是让表格直接提交给PHP的文字,但不管怎么说),那么你可以让该信封为透明的代理玩.,而该代理只是从吉卜赛人的请求中立即将 by转给PHP。 java.net.URLConnection 就此而言,APIC是有用的。

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    HttpURLConnection connection = (HttpURLConnection) new URL("http://example.com/upload.php").openConnection();
    connection.setDoOutput(true); // POST.
    connection.setRequestProperty("Content-Type", request.getHeader("Content-Type")); // This one is important! You may want to check other request headers and copy it as well.

    // Set streaming mode, else HttpURLConnection will buffer everything in Java s memory.
    int contentLength = request.getContentLength();
    if (contentLength > -1) {
        connection.setFixedLengthStreamingMode(contentLength);
     } else {
        connection.setChunkedStreamingMode(1024);
    }

    InputStream input = request.getInputStream();
    OutputStream output = connection.getOutputStream();
    byte[] buffer = new byte[1024]; // Uses only 1KB of memory!
    for (int length = 0; (length = input.read(buffer)) > 0;) {
        output.write(buffer, 0, length);
    }
    output.close();

    InputStream phpResponse = connection.getInputStream(); // Calling getInputStream() is important, it s lazily executed!
    // Do your thing with the PHP response.
}

如果PHP的文字包含 differentmore参数(ain,我只是改动超文本格式,以便它能够直接提交PHP的文字,那么,如果用户使用,Apache Commons 文档上载,以提取上载文档和

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    InputStream fileContent = null;
    String fileContentType = null;
    String fileName = null;

    try {
        List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
        for (FileItem item : items) {
            if (!item.isFormField() && item.getFieldName().equals("file")) { // <input type="file" name="file">
                fileContent = item.getInputStream();
                fileContentType = item.getContentType();
                fileName = FilenameUtils.getName(item.getName());
                break; // If there are no other fields?
            }            
        }
    } catch (FileUploadException e) {
        throw new ServletException("Parsing file upload failed.", e);
    }

    if (fileContent != null) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("http://example.com/upload.php");
        MultipartEntity entity = new MultipartEntity();
        entity.addPart("file", new InputStreamBody(fileContent, fileContentType, fileName));
        httpPost.setEntity(entity);
        HttpResponse phpResponse = httpClient.execute(httpPost);
        // Do your thing with the PHP response.
    }
}

See also:

问题回答

我头顶上的几种解决办法......

  • Use HTTP POST to send the file. Probably not a good idea if the files are large.
  • Use FTP. This easily gives you authentication, handling of large files, etc. Bonus points for using SFTP.
  • Use a program like rsync [over ssh] to migrate the directory contents from one server to the other. Not a good solution if you have disk space concerns since you d be storing the same files twice, once per server.

此外,铭记着如何经常将图像推向你的保护伞。 你们不想试图把100个图像及其传输网络的袖珍藏在记忆中——在此情况下将图像保存在磁盘上。

同样,你可以在你的PHP服务器上安装一个简单的网络服务,接收图像作为后载。

一旦有了这种布局,你就能够利用像HttpClient这样的东西通过邮局发送图像。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签