gpt4 book ai didi

javascript - 将图像/视频从 html 获取到 java servlet 作为 new File()

转载 作者:太空狗 更新时间:2023-10-29 13:14:06 24 4
gpt4 key购买 nike

我想从

获取文件(图片或视频)
<input type='file' id='file_i'/>
// Not this <input type='submit'/>

像这样使用 XMLHttpRequest

function img() {
var fd = new FormData();
fd.append('file', document.getElementById("file_i").files[0]);
var req;
if (window.ActiveXObject) {
req=new ActiveXObject();
} else {
req=new XMLHttpRequest();
}
req.open("post", "Image", true);
req.send(fd);
}

例如。
然后在 servlet 中执行此操作:

new FileInputStream(new File(request.getParameter("file")))

如何检索该文件?提前致谢。

最佳答案

我修好了。在这里:

JavaScript

var fd = new FormData();
fd.append('file', document.getElementById("file_i").files[0]);
var req;
if (window.ActiveXObject) {
req=new ActiveXObject();
} else {
req=new XMLHttpRequest();
}
req.open("post", "Image", true);
req.send(fd);

Java

@MultipartConfig
public class addImage extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
Part filePart = request.getPart("file");
InputStream fileContent = filePart.getInputStream();
}
}

XML

<servlet>
<servlet-name>Add Image</servlet-name>
<servlet-class>servlets.addImage</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Add Image</servlet-name>
<url-pattern>/Image</url-pattern>
</servlet-mapping>

关于javascript - 将图像/视频从 html 获取到 java servlet 作为 new File(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48650327/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com