Android,在tomcat服务器中上传文件

Android,在tomcat服务器中上传文件,第1张

Android,在tomcat服务器中上传文件

这是演示代码

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!!!!!!!!!");}   }   }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5651554.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存