ysgeysge 2018-05-22 07:24 采纳率: 0%
浏览 2839
已结题

java http接口 接收输入流 生成图片,图片大小相等,但是打不开,提示格式不支持,求原因

代码如下:
public static void dealImage(HttpServletRequest request) throws IOException{

// 1.判断当前request消息实体的总长度
int totalBytes = request.getContentLength();
System.out.println("当前数据总长度:" + totalBytes);
// 2.在消息头类型中找出分解符,例如:boundary=----WebKitFormBoundaryeEYAk4vG4tRKAlB6
String contentType = request.getContentType();
int position = contentType.indexOf("boundary=");

    String startBoundary = "--" + contentType.substring(position+"boundary=".length());
    String endBoundary = startBoundary + "--";
    //将request的输入流读入到bytes中
    InputStream inputStream = request.getInputStream();
    DataInputStream dataInputStream = new DataInputStream(inputStream);
    byte[] bytes = new byte[totalBytes];
    dataInputStream.readFully(bytes);
    dataInputStream.close();
    //将字节读入到字符流中
    BufferedReader reader = new BufferedReader(new StringReader(new String(bytes)));

    //开始读取reader(分割form表单内的表单域类型:文本或者文件)

    //记录当前的读取行对应的bytes;
    int temPosition = 0;
    boolean flag = false;
    int end = 0;
    while(true){
        //当读取一次文件信息后
        if(flag){
            bytes = subBytes(bytes, end, totalBytes);
            temPosition = 0;
            reader = new BufferedReader(new StringReader(new String(bytes))); 
        }
        //读取一行的信息:------WebKitFormBoundary5R7esAd459uwQsd5,即:lastBoundary
        String str = reader.readLine();
        System.out.println("this line is:" + str);
        //换行算两个字符
        temPosition += str.getBytes().length + 2;
        //endBoundary:结束
        if(str==null||str.equals(endBoundary)){
            break;
        }
        //表示头信息的开始(一个标签,input,select等)
        if(str.startsWith(startBoundary)){
            //判断当前头对应的表单域类型

            str = reader.readLine(); //读取当前头信息的下一行:Content-Disposition行
            temPosition += str.getBytes().length+2;

            int position1 = str.indexOf("filename="); //判断是否是文件上传
            //such as:Content-Disposition: form-data; name="fileName"; filename="P50611-162907.jpg"
            if(position1 == -1){//表示是普通文本域上传

            }else{//position1!=-1,表示是文件上传
                //解析当前上传的文件对应的name(input标签的name),以及fieldname:文件名
                int position2 = str.indexOf("name=");
                //去掉name与filename之间的"和;以及空格
                String name = str.substring(position2 + "name=".length() + 1, position1-3);
                //去掉两个"
                String filename = str.substring(position1 + "filename=".length() + + 1,str.length() - 1);

                //读取行,such as:Content-Type: image/jpeg,记录字节数,此处两次换行
                temPosition += (reader.readLine().getBytes().length + 4);
                end = locateEnd(bytes, temPosition, totalBytes, endBoundary);
                String path = request.getSession().getServletContext().getRealPath("/");
                DataOutputStream dOutputStream = new DataOutputStream(new FileOutputStream(new File(path + "/"+filename)));

                dOutputStream.write(bytes, temPosition, end-temPosition-2);
                dOutputStream.close();

                flag = true;

            }
        }
    }

}
  • 写回答

1条回答 默认 最新

  • dabocaiqq 2018-05-22 15:44
    关注
    评论

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏