用代码实现lastindexof
时间: 2024-05-01 11:23:31 浏览: 7
lastIndexOf() 是字符串的一个方法,可以用来查找一个字符串在另一个字符串中最后一次出现的位置。下面是一个用 Python 实现 lastIndexOf() 方法的例子:
```python
def lastIndexOf(string, substring):
index = -1
while True:
index = string.find(substring, index + 1)
if index == -1:
return index - 1
return index
```
这个函数使用了 Python 的字符串方法 find(),它可以查找一个字符串在另一个字符串中第一次出现的位置。我们在循环中不断调用 find() 方法,直到找不到目标子串为止。最后,我们返回找到的最后一个目标子串的位置。如果没有找到目标子串,我们返回 -1。
相关问题
jsp上传文件代码实现
JSP上传文件可以使用以下代码实现:
1. HTML页面代码
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>文件上传</title>
</head>
<body>
<form action="upload.jsp" method="post" enctype="multipart/form-data">
<input type="file" name="file"><br>
<input type="submit" value="上传">
</form>
</body>
</html>
```
2. JSP页面代码
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<%
String savePath = "D:/upload"; // 上传文件存储目录
String tempPath = request.getServletContext().getRealPath("/WEB-INF/temp"); // 临时文件存储目录
File tempFile = new File(tempPath);
if (!tempFile.exists()) {
tempFile.mkdirs();
}
String message = "";
try {
// 创建一个DiskFileItemFactory工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
// 设置工厂的缓冲区大小,这里是4KB
factory.setSizeThreshold(4 * 1024);
// 设置上传文件的临时目录
factory.setRepository(tempFile);
// 创建一个ServletFileUpload对象
ServletFileUpload upload = new ServletFileUpload(factory);
// 设置上传文件的大小限制,这里是10MB
upload.setFileSizeMax(10 * 1024 * 1024);
// 解析request对象,并返回一个List集合
List<FileItem> items = upload.parseRequest(request);
for (FileItem item : items) {
if (!item.isFormField()) {
// 获取上传文件的名称
String fileName = item.getName();
// 获取上传文件的后缀名
String suffixName = fileName.substring(fileName.lastIndexOf("."));
// 生成新的文件名
String newFileName = UUID.randomUUID().toString() + suffixName;
// 创建一个文件对象
File saveFile = new File(savePath, newFileName);
// 将上传文件保存到指定路径
item.write(saveFile);
message = "文件上传成功!";
}
}
} catch (Exception e) {
e.printStackTrace();
message = "文件上传失败!";
}
request.setAttribute("message", message);
request.getRequestDispatcher("/result.jsp").forward(request, response);
%>
```
3. result.jsp页面代码
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>上传文件结果</title>
</head>
<body>
<p>${message}</p>
</body>
</html>
```
以上代码实现了文件上传功能,将上传的文件保存到指定目录,并返回上传结果。注意要在web.xml中配置multipart-config元素,指定上传文件的大小限制和临时文件存储目录。
java下载的代码实现
以下是使用Java实现文件下载的代码示例:
```java
import java.io.*;
import java.net.*;
public class FileDownloader {
public static void downloadFile(String fileURL, String saveDir) throws IOException {
URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
int responseCode = httpConn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
String fileName = "";
String disposition = httpConn.getHeaderField("Content-Disposition");
if (disposition != null) {
int index = disposition.indexOf("filename=");
if (index > 0) {
fileName = disposition.substring(index + 10, disposition.length() - 1);
}
} else {
fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1, fileURL.length());
}
InputStream inputStream = httpConn.getInputStream();
String saveFilePath = saveDir + File.separator + fileName;
FileOutputStream outputStream = new FileOutputStream(saveFilePath);
int bytesRead = -1;
byte[] buffer = new byte[4096];
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
inputStream.close();
System.out.println("File downloaded successfully");
} else {
System.out.println("File download failed");
}
httpConn.disconnect();
}
public static void main(String[] args) {
String fileURL = "https://www.example.com/file.pdf";
String saveDir = "C:/Downloads";
try {
downloadFile(fileURL, saveDir);
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
在这个示例中,我们使用Java中的HttpURLConnection类来建立HTTP连接并下载文件。我们从响应中获取文件名,并将其保存到本地文件系统中。如果下载成功,程序将输出“File downloaded successfully”。