有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java安卓一次将许多文件/图像上传到带有参数的servlet

这可以是相同的,也可以看作是重复的,因为上传图像有很多问题。但我想知道如何一次将多个图像上传到servlet。也就是说,如果SD卡中有6个图像,那么所有图像都应该在一个请求中上传,而不是一个接一个地上传。互联网上的大多数样本都是关于一个图像或一个文件的。我想知道图像是否存储在ArrayList中,如何上传

在Servlet中

  List<FileItem> multiparts = new ServletFileUpload(
                                         new DiskFileItemFactory()).parseRequest(request);

                for(FileItem item : multiparts){
                    if(!item.isFormField()){
                        String name = new File(item.getName()).getName();
                        item.write( new File(UPLOAD_DIRECTORY + File.separator + name));
                      }
                }

这一部分工作得很好,因为我使用正常的JSP多部分图像上传进行了测试

在安卓中进行一次图像上传(取自another source):

FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName) );
        URL url = new URL(urlString);
        conn = (HttpURLConnection) url.openConnection();
        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);
        dos = new DataOutputStream( conn.getOutputStream() );
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + exsistingFileName +"\"" + lineEnd);
        dos.writeBytes(lineEnd);
        Log.e("MediaPlayer","Headers are written");
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];
        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);
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) 
            tv.append(inputLine);
        // close streams
        Log.e("MediaPlayer","File is written");
        fileInputStream.close();
        dos.flush();
        dos.close();

此操作仅上载一个图像。如何通过一个POST请求发送多个邮件。?任何其他示例代码或教程请


共 (0) 个答案