如何处理来自Java客户端的服务器端HTTP文件上传 [英] How to handle Server side HTTP file upload from Java client

查看:159
本文介绍了如何处理来自Java客户端的服务器端HTTP文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



客户端:Java使用HTTP发布
服务器:Java Servlet



我在这里添加了客户端编码。但是,我不知道服务器端处理。请用代码片段帮助我。

 私人字符串标记=UPLOADER; 
private String urlString =http://192.168.42.140:8080/sampweb;
HttpURLConnection conn;
String exsistingFileName =/sdcard/log.txt;
String lineEnd =\r\\\
;
String twoHyphens = - ;
String boundary =*****;
尝试{
// ------------------客户请求

Log.e(Tag,Inside second Method );

FileInputStream fileInputStream = new FileInputStream(new File(
exsistingFileName));

//打开到Servlet的URL连接
$ b $ URL URL = new URL(urlString);

//打开一个HTTP连接到URL

conn =(HttpURLConnection)url.openConnection();

//允许输入
conn.setDoInput(true);

//允许输出
conn.setDoOutput(true);

//不要使用缓存副本。
conn.setUseCaches(false);

//使用post方法。
conn.setRequestMethod(POST);

conn.setRequestProperty(Connection,Keep-Alive);

conn.setRequestProperty(Content-Type,
multipart / form-data; boundary =+ boundary);

DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

dos.writeBytes(twoHyphens + boundary + lineEnd);
dos
.writeBytes(Content-Disposition:post-data; name = uploadedfile; filename =
+ exsistingFileName ++ lineEnd);
dos.writeBytes(lineEnd);

Log.e(Tag,Headers are written);

//创建最大大小的缓冲区
$ b $ int bytesAvailable = fileInputStream.available();
int maxBufferSize = 1000;
// int bufferSize = Math.min(bytesAvailable,maxBufferSize);
byte [] buffer = new byte [bytesAvailable];

//读取文件并写入表单...

int bytesRead = fileInputStream.read(buffer,0,bytesAvailable);

while(bytesRead> 0){
dos.write(buffer,0,bytesAvailable);
bytesAvailable = fileInputStream.available();
bytesAvailable = Math.min(bytesAvailable,maxBufferSize);
bytesRead = fileInputStream.read(buffer,0,bytesAvailable);


//发送文件数据后所需的多部分表单数据...

dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

//关闭流
Log.e(Tag,File is written);
fileInputStream.close();
dos.flush();
dos.close();
$ b} catch(MalformedURLException ex){
Log.e(Tag,error:+ ex.getMessage(),ex);
}

catch(IOException ioe){
Log.e(Tag,error:+ ioe.getMessage(),ioe);


try {
BufferedReader rd = new BufferedReader(new InputStreamReader(conn
.getInputStream()));
字符串行; ((line = rd.readLine())!= null){
Log.e(Dialoge Box,Message:+ line);
}
rd.close();
$ b} catch(IOException ioex){
Log.e(MediaPlayer,error:+ ioex.getMessage(),ioex);




$ b我是新来的服务器编程。如果我犯了什么错误,请澄清我。谢谢!

解决方案

最好的库实现这个目前仍然是Apache Commons 文件上传。这是有据可查,易于使用。如果遇到任何问题,请先查看常见问题解答


I want to upload a file from a client to a server.

Client: Java using HTTP post Server: Java Servlet

I have added the client side coding here. But, i don't have any idea with the server side processing. Please do help me with a code snippet.

 private String Tag = "UPLOADER";
    private String urlString = "http://192.168.42.140:8080/sampweb";
    HttpURLConnection conn;
    String exsistingFileName = "/sdcard/log.txt";
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";
    try {
        // ------------------ CLIENT REQUEST

        Log.e(Tag, "Inside second Method");

        FileInputStream fileInputStream = new FileInputStream(new File(
                exsistingFileName));

        // open a URL connection to the Servlet

        URL url = new URL(urlString);

        // Open a HTTP connection to the URL

        conn = (HttpURLConnection) url.openConnection();

        // Allow Inputs
        conn.setDoInput(true);

        // Allow Outputs
        conn.setDoOutput(true);

        // Don't use a cached copy.
        conn.setUseCaches(false);

        // Use a post method.
        conn.setRequestMethod("POST");

        conn.setRequestProperty("Connection", "Keep-Alive");

        conn.setRequestProperty("Content-Type",
                "multipart/form-data;boundary=" + boundary);

        DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos
                .writeBytes("Content-Disposition: post-data; name=uploadedfile;filename="
                        + exsistingFileName + "" + lineEnd);
        dos.writeBytes(lineEnd);

        Log.e(Tag, "Headers are written");

        // create a buffer of maximum size

        int bytesAvailable = fileInputStream.available();
        int maxBufferSize = 1000;
        // int bufferSize = Math.min(bytesAvailable, maxBufferSize);
        byte[] buffer = new byte[bytesAvailable];

        // read file and write it into form...

        int bytesRead = fileInputStream.read(buffer, 0, bytesAvailable);

        while (bytesRead > 0) {
            dos.write(buffer, 0, bytesAvailable);
            bytesAvailable = fileInputStream.available();
            bytesAvailable = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bytesAvailable);
        }

        // send multipart form data necessary after file data...

        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

        // close streams
        Log.e(Tag, "File is written");
        fileInputStream.close();
        dos.flush();
        dos.close();

    } catch (MalformedURLException ex) {
        Log.e(Tag, "error: " + ex.getMessage(), ex);
    }

    catch (IOException ioe) {
        Log.e(Tag, "error: " + ioe.getMessage(), ioe);
    }

    try {
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn
                .getInputStream()));
        String line;
        while ((line = rd.readLine()) != null) {
            Log.e("Dialoge Box", "Message: " + line);
        }
        rd.close();

    } catch (IOException ioex) {
        Log.e("MediaPlayer", "error: " + ioex.getMessage(), ioex);
    }
}

I'm new to the server programming. If i have done any mistakes do clarify me. Thanks!

解决方案

The best library to achieve this is still the Apache Commons File Upload. It is well documented and easy to use. If you ran into any issues, please check FAQ first.

这篇关于如何处理来自Java客户端的服务器端HTTP文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆