文件上传无法使用Android发送内容处理

时间:2016-10-30 19:05:36

标签: java android rest android-asynctask jersey-2.0

我有一个球衣休息服务器,接受下面代码的文件。

从Web浏览器发送时,能够从内容处理中接收文件名

然而,在Android上,它无法发送文件名,但能够发送文件内容。

这是球衣服务器 -

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(

    @FormDataParam("file") InputStream uploadedInputStream,
    @FormDataParam("file") FormDataContentDisposition fileDetail) {
            newa.NewClass newca = new newa.NewClass();
            try {
            javax.naming.Context env = (javax.naming.Context)new InitialContext().lookup("java:comp/env");
    String uploadedFileLocation = (String)env.lookup("storeImage") + fileDetail.getFileName();
    // save it
            newca.insert_exception("uploadFile name: "  , uploadedFileLocation);
    writeToFile(uploadedInputStream, uploadedFileLocation);
    String output = "File uploaded to : " + uploadedFileLocation;
    return Response.status(200).entity(output).build();
            } catch(NamingException ne) {
                newca.insert_exception("uploadFile Exception"  , ne.toString());
                return Response.status(200).entity("Error").build();
            }
}

private void writeToFile(InputStream uploadedInputStream,
    String uploadedFileLocation) {

    try {
        OutputStream out = new FileOutputStream(new File(uploadedFileLocation));
        int read = 0;
        byte[] bytes = new byte[1024];

        out = new FileOutputStream(new File(uploadedFileLocation));
        while ((read = uploadedInputStream.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        out.flush();
        out.close();

    } catch (IOException e) {

        e.printStackTrace();
    }

}

能够使用文件名上传文件的普通html页面 -

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Upload Test</title>
</head>
<body>
    <form action="rest/hello/upload" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="file" id="file">
<input type="submit" value="Upload Image" name="submit">

    

无法发送文件名但能够发送文件内容的Android代码 -

private String server;

    public AsyncHttpPostTask(final String server) {
        this.server = server;
    }

    @Override
    protected String doInBackground(String... params) {
        Log.d("ListView1", "doInBackground");
        String iFileName = "ovicam_temp_vid.mp4";
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        String Tag="fSnd";
        URL connectURL;
        try {



            connectURL = new URL(server);

            HttpURLConnection conn = (HttpURLConnection)connectURL.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: form-data; name=\"file\""+ lineEnd);
            dos.writeBytes(lineEnd);
            dos.writeBytes("file.jpeg");
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + lineEnd);



            dos.writeBytes("Content-Disposition: form-data; name=\"description\""+ lineEnd);
            dos.writeBytes(lineEnd);
            dos.writeBytes("Hello");
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + lineEnd);



            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"file\";file=\"" + "someString" +"\"" + lineEnd);
            dos.writeBytes(lineEnd);

            Log.e(Tag,"Headers are written");
            Log.d("ListView1", "File: " + params[0]);
            FileInputStream fileInputStream = new FileInputStream(params[0]);
            // create a buffer of maximum size
            int bytesAvailable = fileInputStream.available();

            int maxBufferSize = 1024;
            int bufferSize = Math.min(bytesAvailable, maxBufferSize);
            byte[ ] buffer = new byte[bufferSize];

            // read file and write it into form...
            int bytesRead = fileInputStream.read(buffer, 0, bufferSize);

            while (bytesRead > 0)
            {
                dos.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable,maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0,bufferSize);
            }
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

            // close streams
            fileInputStream.close();

            dos.flush();

            Log.d("ListView1",  "File Sent, Response: " + String.valueOf(conn.getResponseCode()));

            InputStream is = conn.getInputStream();

            // retrieve the response from server
            int ch;

            StringBuffer b =new StringBuffer();
            while( ( ch = is.read() ) != -1 ){ b.append( (char)ch ); }
            String s=b.toString();
            Log.d("ListView1" , s);
            dos.close();


        } catch (IOException ie ) {
            Log.d("ListView1", "Upload Files Response:::" + ie.toString());
        }

1 个答案:

答案 0 :(得分:0)

它与android中的标题有关 -

应该简单 -

.....
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\"" + "gold1.jpeg" +"\"" + lineEnd);
dos.writeBytes(lineEnd);
Log.e(Tag,"Headers are written");
.....