private String fileUploader(HttpServletRequest req){
Part part=req.getPart("file");//type为file的表单
String tmpfp = req.getParameter("filepath");//本地保存路径
//判断是否为合法路径
if(!tmpfp.startsWith("/")){
tmpfp = "/" + tmpfp;
};
//文件存储路径 (与输入的filePath合成之后为文件完整的路径)
String fileSavePath = new File(this.getServletConfig().getServletContext().getRealPath("/")).getParent() +
"/upload/";
//String srcip = req.getRemoteAddr();
//创建文件实体
File f = new File(fileSavePath, tmpfp);
if(!f.exists()){
new File(f.getParent()).mkdirs();
f.createNewFile();
}
//写入文件
part.write(f.getPath());
//文件写入失败,或者上传空文件则返回无效路径做判断
if(f.length()== 0)return "";
serlvet3.0 文件上传
最新推荐文章于 2022-08-25 20:15:02 发布