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

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
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 机器学习教材中的例题询问
  • ¥15 求.net core 几款免费的pdf编辑器
  • ¥15 C# P/Invoke的效率问题
  • ¥20 thinkphp适配人大金仓问题
  • ¥20 Oracle替换.dbf文件后无法连接,如何解决?(相关搜索:数据库|死循环)
  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)