Java上传文件实现更换头像

本博文主要说,用户如何更换头像的操作

 

1.首先要有一个util的工具类(直接用就可以)上传文件需要的jar包,在最下方。

package com.hp.factory;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class FileUploadUtil {
//文件接收
private static final long serialVersionUID = -4187075130535308117L;
private boolean isMultipart;
private int maxFileSize = 1024 * 1024 * 10;
private int maxMemSize = 100 * 1024;

private static FileUploadUtil fu = null;
public static FileUploadUtil getFu() {
if(fu == null) {
fu = new FileUploadUtil();
}
return fu;
}

public String upload(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 检查是否有一个文件上传请求
isMultipart = ServletFileUpload.isMultipartContent(request);
String result = "";
if (!isMultipart) {
result = "not_found";
return result;
}
DiskFileItemFactory factory = new DiskFileItemFactory();
// 文件大小的最大值将被存储在内存中
factory.setSizeThreshold(maxMemSize);

//path是下载到的地址
String path = "D:\\Tomcat-9\\apache-tomcat-9.0.34\\webapps\\hui\\";  //
factory.setRepository(new File(path));
//System.out.println(path);
// 创建一个新的文件上传处理程序
ServletFileUpload upload = new ServletFileUpload(factory);
// 允许上传的文件大小的最大值
upload.setSizeMax(maxFileSize);
try {
// 解析请求,获取文件项
List fileItems = upload.parseRequest(request);
// 处理上传的文件项
Iterator i = fileItems.iterator();
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
if (!fi.isFormField()) {
// 获取上传文件的参数
String fieldName = fi.getFieldName();
String fileName = fi.getName();
String contentType = fi.getContentType();
boolean isInMemory = fi.isInMemory();
long sizeInBytes = fi.getSize();
// 写入文件
path = path + System.currentTimeMillis() / 1000 + ".jpg"; 
File file = new File(path);
fi.write(file);
}
}
result = path;
} catch (Exception ex) {
System.out.println("ex:" + ex.getMessage());
result = "error";
}

return result;
}

}

2.jsp页面头像的修改(表单提交)

<form id="loginForm" enctype="multipart/form-data" action="img.upload" method="post" >
<table>
<tr><td><input type="file" name="img"/></td>

<td><button type="submit">修改</button><tr><td>
</table>
</form>

3.servlet类,这里写入到了post里面

 

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String str = FileUploadUtil.getFu().upload(request, response);  //调用了util工具类
if(str.equals("not_found")) {//不是文件上传
//response.getWriter().write("error");
}else if(str.equals("error")) {//上传失败
//response.getWriter().write("errore");
}else {
//把路径存数据库去,然后前台读取
String adminLoad = str.replace('\\','/').substring(40);//这里是,替换并分割出来,替换的是转义符,分割的是webapps的目录一下的目录(这里不包括webapps)

String adminName = (String) request.getSession().getAttribute("adminName");     //这里是我要修改的用户的账号
try {
managerDao.adminLoad(adminLoad, adminName);  //这里是修改头像在mysql中的字段
response.sendRedirect("/project01/login.jsp");//修改成功跳转的地址
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

 

4.jsp页面展示的语句

//src="${adminLoad}" 是我们登录成功时,直接request.getSession().setAttribute("adminLoad", img);

<li ><img style="border-radius: 30px; border: 2px; margin-top: 30%;" width="30px" height="30px" src="${adminLoad}"/></li>

 

 

链接https://pan.baidu.com/s/19UCxPr5rq2c0y7bB1Omjwg 提取码:nubu

上面的内容有什么不对的地方,请大家多多指教!

 

posted @ 2020-10-28 19:31  辉哥哥~  阅读(1524)  评论(0编辑  收藏  举报