java stream filter 包含name等于l 或者等li
时间: 2023-07-05 20:18:28 浏览: 87
可以使用如下代码实现:
```java
List<Person> people = ... // 假设这里有一个Person对象的列表
List<Person> filteredPeople = people.stream()
.filter(person -> person.getName().matches("l(i)?"))
.collect(Collectors.toList());
```
这段代码会筛选出`name`属性为"l"或者"li"的`Person`对象,并将它们放入一个新的列表中。其中,`matches()`方法用于判断字符串是否匹配正则表达式,正则表达式`"l(i)?"`表示匹配以"l"开头,后面跟0个或1个"i"的字符串。当然,你需要将代码中的`Person`替换成你自己的实体类名,并根据实际情况修改属性名和正则表达式。
相关问题
用户能进行个人文件管理,能上传和下载图片、文档,可以对相册图片和文档进行查看、删除等操作的Javaweb代码
以下是一个简单的 Javaweb 文件管理系统的代码示例,包括上传、下载、查看、删除功能:
1. 首先,在项目的 web.xml 文件中配置以下 servlet 和 filter:
```xml
<servlet>
<servlet-name>FileUploadServlet</servlet-name>
<servlet-class>com.example.FileUploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FileUploadServlet</servlet-name>
<url-pattern>/file/upload</url-pattern>
</servlet-mapping>
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>com.example.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/file/*</url-pattern>
</filter-mapping>
```
2. 创建一个 FileUploadServlet 类,用于处理文件上传请求:
```java
public class FileUploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String UPLOAD_DIRECTORY = "uploads";
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 创建文件上传工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
// 设置缓冲区大小
factory.setSizeThreshold(1024 * 1024);
// 设置上传目录
File uploadDir = new File(getServletContext().getRealPath("") + File.separator + UPLOAD_DIRECTORY);
if (!uploadDir.exists()) {
uploadDir.mkdir();
}
factory.setRepository(uploadDir);
// 创建文件上传处理器
ServletFileUpload upload = new ServletFileUpload(factory);
// 设置上传文件大小限制
upload.setFileSizeMax(1024 * 1024 * 10); // 10 MB
// 解析请求
List<FileItem> items = null;
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
// 处理上传的文件
for (FileItem item : items) {
if (!item.isFormField()) {
String fileName = new File(item.getName()).getName();
String filePath = getServletContext().getRealPath("") + File.separator + UPLOAD_DIRECTORY + File.separator + fileName;
File uploadedFile = new File(filePath);
try {
item.write(uploadedFile);
} catch (Exception e) {
e.printStackTrace();
}
}
}
response.sendRedirect(request.getContextPath() + "/file/list");
}
}
```
3. 创建一个 FileListServlet 类,用于显示文件列表和处理文件下载请求:
```java
public class FileListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String UPLOAD_DIRECTORY = "uploads";
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取上传目录的绝对路径
String uploadPath = getServletContext().getRealPath("") + File.separator + UPLOAD_DIRECTORY;
// 创建上传目录文件对象
File uploadDir = new File(uploadPath);
// 获取上传目录下的所有文件对象
File[] files = uploadDir.listFiles();
// 将文件对象数组转换为文件名数组
String[] fileNames = new String[files.length];
for (int i = 0; i < files.length; i++) {
fileNames[i] = files[i].getName();
}
// 将文件名数组存入请求属性中
request.setAttribute("fileNames", fileNames);
// 转发到文件列表页面
request.getRequestDispatcher("/file/list.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取下载文件名
String fileName = request.getParameter("fileName");
// 获取下载文件对象
String filePath = getServletContext().getRealPath("") + File.separator + UPLOAD_DIRECTORY + File.separator + fileName;
File downloadFile = new File(filePath);
// 设置响应头,告诉浏览器下载文件
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
response.setHeader("Content-Length", String.valueOf(downloadFile.length()));
// 将文件内容写入响应输出流中
FileInputStream in = new FileInputStream(downloadFile);
OutputStream out = response.getOutputStream();
byte[] buffer = new byte[1024];
int length = 0;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.flush();
}
}
```
4. 创建一个 LoginFilter 类,用于检查用户是否已登录:
```java
public class LoginFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
HttpSession session = httpRequest.getSession(false);
boolean isLoggedIn = (session != null && session.getAttribute("user") != null);
String loginURI = httpRequest.getContextPath() + "/login.jsp";
boolean isLoginRequest = httpRequest.getRequestURI().equals(loginURI);
boolean isLoginPage = httpRequest.getRequestURI().endsWith("login.jsp");
if (isLoggedIn && (isLoginRequest || isLoginPage)) {
httpResponse.sendRedirect(httpRequest.getContextPath() + "/file/list");
} else if (isLoggedIn || isLoginRequest || isLoginPage) {
chain.doFilter(request, response);
} else {
httpResponse.sendRedirect(loginURI);
}
}
public void destroy() {}
}
```
5. 创建一个文件列表页面 list.jsp,用于显示文件列表和处理文件上传和下载:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>文件列表</title>
</head>
<body>
<h1>文件列表</h1>
<% if (request.getAttribute("fileNames") != null) { %>
<ul>
<% for (String fileName : (String[]) request.getAttribute("fileNames")) { %>
<li>
<%= fileName %>
<form method="post" action="<%= request.getContextPath() %>/file/download">
<input type="hidden" name="fileName" value="<%= fileName %>">
<button type="submit">下载</button>
</form>
<form method="post" action="<%= request.getContextPath() %>/file/delete">
<input type="hidden" name="fileName" value="<%= fileName %>">
<button type="submit">删除</button>
</form>
</li>
<% } %>
</ul>
<% } else { %>
<p>没有上传的文件</p>
<% } %>
<h2>上传文件</h2>
<form method="post" action="<%= request.getContextPath() %>/file/upload" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit">上传</button>
</form>
</body>
</html>
```
6. 创建一个文件删除页面 delete.jsp,用于处理文件删除请求:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>删除文件</title>
</head>
<body>
<h1>删除文件</h1>
<p>确定要删除文件 <%= request.getParameter("fileName") %> 吗?</p>
<form method="post" action="<%= request.getContextPath() %>/file/do_delete">
<input type="hidden" name="fileName" value="<%= request.getParameter("fileName") %>">
<button type="submit">是</button>
<a href="<%= request.getContextPath() %>/file/list">否</a>
</form>
</body>
</html>
```
7. 创建一个 FileDeleteServlet 类,用于处理文件删除请求:
```java
public class FileDeleteServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String UPLOAD_DIRECTORY = "uploads";
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取要删除的文件名
String fileName = request.getParameter("fileName");
// 获取要删除的文件对象
String filePath = getServletContext().getRealPath("") + File.separator + UPLOAD_DIRECTORY + File.separator + fileName;
File deleteFile = new File(filePath);
// 删除文件
if (deleteFile.exists()) {
deleteFile.delete();
}
response.sendRedirect(request.getContextPath() + "/file/list");
}
}
```
这个文件管理系统包括上传、下载、查看、删除文件的功能,可以根据需要进行扩展和修改。
阅读全文