代码仅供参考哈 以下是servlel dopost方法中的代码
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//这两句代码为了不让网页里的中文乱码
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
//获得存储的文件域
Part part = request.getPart("pic");
//获得存储的文件名
String fileName = part.getSubmittedFileName();
//给一个绝对路径,存到这块
String savaPath = getServletContext().getRealPath("/images");
File f =new File(savaPath+"/");
//如果路径不存在就创建一个
if(!f.exists()) {
f.mkdir();
}
part.write(savaPath+"/"+fileName);
PrintWriter out = response.getWriter();
out.println("Success!");
}
补充一个jsp文件的代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>
<form action="uploadServlet" method="post" enctype="multipart/form-data"/>
<input type="file" name="pic" />
<input type="submit" value="上传照片"/>
</form>
</body>
</html>
servletrequest接口实现文件的下载
最新推荐文章于 2022-12-13 16:35:07 发布