YYPOST群发软件 发表于 2025-2-11 09:00:19

AIWROK软件教程:okHttp上传文件的使用方法

<p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">以这个网站为例子<br style="box-sizing: border-box;"><a href="https://ocr.wdku.net/" style="color: rgb(65, 131, 196); text-decoration-line: none; transition: color 0.08s ease-out 0s; box-sizing: border-box; background: 0px 0px;">https://ocr.wdku.net/</a><br style="box-sizing: border-box;">我们首先抓包上传的参数<br style="box-sizing: border-box;"><ignore_js_op></ignore_js_op></p><br style="box-sizing: border-box;"><ignore_js_op></ignore_js_op><br><p></p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">通过以上抓包我们分析出 几个关键点<br style="box-sizing: border-box;">第一:上传的url是&nbsp;<a href="https://ocr.wdku.net/upload" style="color: rgb(65, 131, 196); text-decoration-line: none; transition: color 0.08s ease-out 0s; box-sizing: border-box; background: 0px 0px;">https://ocr.wdku.net/upload</a><br style="box-sizing: border-box;">第二:二进制的参数名字是 file<br style="box-sizing: border-box;">第三:其他参数<br style="box-sizing: border-box;">user,name,from,fileName</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">现在使用okhttp上传方法以此填写参数</p><pre style="overflow-wrap: normal; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; box-sizing: border-box; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="lang-javascript hljs" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> http1=<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> okHttp();
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> fromdata= <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> map()<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">//其他参数使用map格式</span>
fromdata.add(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"user"</span>,<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"default"</span>)
fromdata.add(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"name"</span>,<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"t019bf9f2ac19af88b0.webp.png_1739158225246"</span>)
fromdata.add(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"from"</span>,<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"select"</span>)
fromdata.add(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"fileName"</span>,<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"t019bf9f2ac19af88b0.webp.png"</span>)

<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">//上传提交这个网页不需要设置cookie和header所以简单,如果需要需要header或者cookie的需处理下header</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> res=http1.uploadFile(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"https://ocr.wdku.net/upload"</span>,<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"file"</span>,<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"sdcard/1.png"</span>,fromdata)
printl(res)
</code></pre><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">下面是代码的详细说明:<br style="box-sizing: border-box;"><ignore_js_op></ignore_js_op></p><br><p></p><ol style="padding-left: 2em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">var http1=new okHttp();</code>&nbsp;这行代码创建了一个<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">okHttp</code>对象实例,这个对象将用于执行HTTP请求操作。</p></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">var fromdata= new map()</code>&nbsp;这行代码创建了一个<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">map</code>对象实例,用于存储将要随文件上传一起发送的表单数据。在JavaScript中,通常会使用对象字面量(<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">{}</code>)或者<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">Map</code>对象来存储键值对,但这里的<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">map</code>似乎是自定义的类或者库的一部分,用于实现类似的功能。</p></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">fromdata.add("user","default")</code>&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">fromdata.add("name","t019bf9f2ac19af88b0.webp.png_1739158225246")</code>&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">fromdata.add("from","select")</code>&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">fromdata.add("fileName","t019bf9f2ac19af88b0.webp.png")</code>&nbsp;这些行代码向<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">fromdata</code>对象中添加了键值对,这些数据将会作为表单数据与文件一起发送到服务器。键分别是”user”, “name”, “from”, “fileName”,对应的值分别是”default”, “t019bf9f2ac19af88b0.webp.png_1739158225246”, “select”, 和 “t019bf9f2ac19af88b0.webp.png”。</p></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">var res=http1.uploadFile("https://ocr.wdku.net/upload","file","sdcard/1.png",fromdata)</code>&nbsp;这行代码使用<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">okHttp</code>对象的<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">uploadFile</code>方法向指定的URL(”<a href="https://ocr.wdku.net/upload%22%EF%BC%89%E4%B8%8A%E4%BC%A0%E6%96%87%E4%BB%B6%E3%80%82%60uploadFile%60%E6%96%B9%E6%B3%95%E7%9A%84%E5%8F%82%E6%95%B0%E8%AF%B4%E6%98%8E%E5%A6%82%E4%B8%8B%EF%BC%9A" style="color: rgb(65, 131, 196); text-decoration-line: none; transition: color 0.08s ease-out 0s; box-sizing: border-box; background: 0px 0px;">https://ocr.wdku.net/upload")上传文件。`uploadFile`方法的参数说明如下:</a></p></li><ul style="margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style: disc; box-sizing: border-box;">第一个参数是目标URL。</li><li style="list-style: disc; box-sizing: border-box;">第二个参数是文件的字段名,服务器会根据这个字段名来识别上传的文件。</li><li style="list-style: disc; box-sizing: border-box;">第三个参数是本地文件的路径,这里指定的是”sdcard/1.png”,即SD卡下的1.png文件。</li><li style="list-style: disc; box-sizing: border-box;">第四个参数是之前创建的<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">fromdata</code>对象,包含了随文件一起提交的表单数据。</li></ul><li><ignore_js_op></ignore_js_op></li></ol><p></p>
页: [1]
查看完整版本: AIWROK软件教程:okHttp上传文件的使用方法