httpurlconnection上传多图片后文件流保存图片无法打开

//上传多个图片文件的APPLET
public void uploadMutilFile(String []uploadFiles, String actionUrl){
String BOUNDARY = java.util.UUID.randomUUID().toString();
String PREFIX = "--", LINEND = "\r\n";
String MULTIPART_FROM_DATA = "multipart/form-data";
String CHARSET = "UTF-8";
try{
URL uri = new URL(actionUrl);
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
conn.setReadTimeout(10* 1000); // 缓存的最长时间
conn.setDoInput(true);// 允许输入
conn.setDoOutput(true);// 允许输出
conn.setUseCaches(false); // 不允许使用缓存
conn.setRequestMethod("POST");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("Charsert", "UTF-8");
conn.setRequestProperty("Content-Type", "text/html");

        DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
        InputStream in = null;
     // 发送文件数据
        if(uploadFiles.length>0){
            for(int i=0;i<uploadFiles.length;i++){
                File uploadFile = new File(uploadFiles[i]);
                StringBuilder sb1 = new StringBuilder();
                sb1.append(PREFIX);
                sb1.append(BOUNDARY);
                sb1.append(LINEND);

                sb1.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + uploadFile.getName() + "\"" + LINEND);
                //sb1.append("Content-Type: application/octet-stream; charset=" + CHARSET + LINEND);
                sb1.append("Content-Type: image/jpeg"+ LINEND);
                sb1.append(LINEND);
                System.out.println(sb1.toString());
                outStream.writeBytes(sb1.toString());

                FileInputStream is = new FileInputStream(uploadFile);
                byte[] buffer = new byte[(int)uploadFile.length()];
                int len = 0;
                while ((len = is.read(buffer)) != -1)
                {
                    outStream.write(buffer, 0, len);
                }

                outStream.write(LINEND.getBytes());

                is.close();

            }

            // 请求结束标志
            outStream.writeBytes(PREFIX + BOUNDARY + PREFIX + LINEND);
            outStream.flush();
            outStream.close();
            // 得到响应码
            int res = conn.getResponseCode();
            if (res == 200)
            {
                in = conn.getInputStream();
                int ch;
                StringBuilder sb2 = new StringBuilder();
                while ((ch = in.read()) != -1)
                {
                    sb2.append((char) ch);
                }
                System.out.println(sb2.toString());
            }

            conn.disconnect();
        }

    }catch(Exception e){
        e.printStackTrace();
    }

}





//调用的ACTION
public String SaveImgAction(){
    request = ServletActionContext.getRequest();

    /*try{
        ServletInputStream mRead = request.getInputStream();
        byte[] bytes = new byte[1024];
        int len = mRead.read(bytes,0,1024);
        StringBuffer str = new StringBuffer();
        while(len != -1){
            String str1 = new String(bytes,0,1024);
            str.append(str1);
            len = mRead.read(bytes,0,1024);
        }
        System.out.println(new String(str.toString().getBytes(),"UTF-8"));
    }catch(Exception e){
        e.printStackTrace();
    }*/
    try{
        int totalbytes = request.getContentLength();
        byte[] dataOrgin = new byte[totalbytes];
        byte[] b = new byte[totalbytes];
        Map<String,byte[]>mapfile = new HashMap<String , byte[]>();
        DataInputStream in = new DataInputStream(request.getInputStream());
        in.readFully(dataOrgin);
        in.close();
        String reqcontent = new String(dataOrgin);
        BufferedReader reqbuf = new BufferedReader(new StringReader(reqcontent));
        String s = reqbuf.readLine();
        String startStr = s;
        System.out.println(s);
        String fname="";
        while((s=reqbuf.readLine())!=null){
            if(s.startsWith("Content-Disposition")){
                String filename = s.substring(s.indexOf("filename=")+"filename=".length()+1,s.length()-1);
                fname = filename;
            }
            if(s.startsWith("Content-Type")){
                s= reqbuf.readLine();
                s=reqbuf.readLine();
                System.out.println(s.getBytes().length);

                File f = new File("E:"+File.separator+fname);
                DataOutputStream fileout = new DataOutputStream(new FileOutputStream(f));
                byte []arr = s.getBytes();
                fileout.write(s.getBytes());
                fileout.close();
                //mapfile.put(fname, arr);
            }
            if(s.startsWith(startStr+"--")){
                System.out.println("结束");
                break;
            }
        }
    }catch(Exception e){
        e.printStackTrace();
    }
    return null;
}




通过JS调用applet方法
<script type="text/javascript">
     function fz(){
        var arr=[];
        arr[0]="D:\\test3.jpg";
        arr[1]="D:\\test4.jpg";
        var str = "http://192.168.253.61:7001/TestProject/saveImg.action";
        document.appTest.uploadMutilFile(arr , str);

     }
</script>

    结果:上传的文件大小跟ACTION获取保存到服务器上的文件大小不一样,无法打开。各位大侠帮忙看看啊

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问