public static void main(String[] args) throws Exception{
HttpServerProvider httpServerProvider = HttpServerProvider.provider()
InetSocketAddress addr = new InetSocketAddress(7778)
HttpServer httpServer = httpServerProvider.createHttpServer(addr, 1)
httpServer.createContext("/myapp/", new MyHttpHandler())
httpServer.setExecutor(null)
httpServer.start()
System.out.println("started")
}
static class MyHttpHandler implements HttpHandler{
public void handle(HttpExchange httpExchange) throws IOException {
String response = "Hello world!"
httpExchange.sendResponseHeaders(200, response.length())
OutputStream out = httpExchange.getResponseBody()
out.write(response.getBytes())
out.close()
}
}
然后,在浏览器中访问 http://localhost:7778/myapp/
你用的servlet 还是别的框架?
选POST
选form-data
选body
选File
选文件
Send
// commons fileupload组件的情况下,servlet接收的数据只能是type=file表单元素类型,那么获取type=text类型,就可以使用parseRequest(request)来获取list,fileitem,判断isFormField,为true非file类型的。就可以处理了。下面是处理的部分代码:DiskFileItemFactory factory = new DiskFileItemFactory()factory.setSizeThreshold(1024*1024)
String dirtemp = "c:"
File filedir = new File(dirtemp + "filetemp")
String str = nullif(!filedir.exists())filedir.mkdir()factory.setRepository(filedir)
ServletFileUpload upload = new ServletFileUpload(factory)
List list = upload.parseRequest(request)for(
int i = 0i<list.size()i++)
{
FileItem item = (FileItem) list.get(i)
if (item.isFormField()) {
System.out.println(item.getString())
} else {
String filename = item.getName()
item.write(new File(request.getRealPath(dir), filename))
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)