gpt4 book ai didi

java - Android,在tomcat服务器上传一个文件

转载 作者:行者123 更新时间:2023-11-28 22:38:32 24 4
gpt4 key购买 nike

我正在寻找可以从 tomcat 服务器中的 android 应用程序快速上传图像的代码。

目前我还没有找到放置在 tomcat 服务器(servlet)中的 java 代码

最佳答案

这是演示代码。

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.util.*;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class UploadServlet extends HttpServlet{
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.print("Request content length is " + request.getContentLength() + "<br/>");
out.print("Request content type is " + request.getHeader("Content-Type") + "<br/>");
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(isMultipart){
ServletFileUpload upload = new ServletFileUpload();
try{
FileItemIterator iter = upload.getItemIterator(request);
FileItemStream item = null;
String name = "";
InputStream stream = null;
while (iter.hasNext()){
item = iter.next();
name = item.getFieldName();
stream = item.openStream();
if(item.isFormField()){out.write("Form field " + name + ": "
+ Streams.asString(stream) + "<br/>");}
else {
name = item.getName();
if(name != null && !"".equals(name)){
String fileName = new File(item.getName()).getName();
out.write("Client file: " + item.getName() + " <br/>with file name "
+ fileName + " was uploaded.<br/>");
File file = new File(getServletContext().getRealPath("/" + fileName));
FileOutputStream fos = new FileOutputStream(file);
long fileSize = Streams.copy(stream, fos, true);
out.write("Size was " + fileSize + " bytes <br/>");
out.write("File Path is " + file.getPath() + "<br/>");
}
}
}
} catch(FileUploadException fue) {out.write("fue!!!!!!!!!");}
}
}
}

关于java - Android,在tomcat服务器上传一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10093740/

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