服务端代码,主要是用了servlet3.0中的Part类来接受并保存文件,一开始没有加@MultipartConfig注解,一直不成功,唉。。。
maxFileSize=1024*1024*50, // 50 MB
maxRequestSize=1024*1024*100)
public class shareServlet extends HttpServlet{
@Override
public void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
response(req, resp);
}
public void response(HttpServletRequest req, HttpServletResponse resp){
resp.setContentType("text/html;charset=utf-8");
try {
Part part=req.getPart("file");
part.write("d:/aaaaa");
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (ServletException e) {
e.printStackTrace();
}
}
}
客服端的表单记得加上enctype="multipart/form-data"属性
客户端代码示例
<form action="/a/text" method="post" enctype="multipart/form-data" >
文件: <input type="file" name="file" />
<input type="submit" value="上传" />
</form>