String ext=name.substring(name.lastIndexOf(".")+1);
时间: 2024-05-28 16:11:51 浏览: 12
这段代码的作用是获取文件名中的扩展名(后缀名)。首先,使用 `lastIndexOf()` 函数获取文件名中最后一个点号(.)的下标位置,然后加1得到扩展名的起始位置。接着,使用 `substring()` 函数截取从起始位置开始到字符串结尾的子字符串,即为扩展名。最终,将得到的扩展名赋值给变量 `ext`。
相关问题
java如何判断jsp页面form表单中类型为file的input是否有选中文件或文件夹
在JSP中判断form表单中类型为file的input是否有选中文件或文件夹,可以通过以下步骤实现:
1. 在JSP页面中添加一个form表单,并在其中添加一个type为file的input元素。
2. 在JSP页面中添加一个提交按钮,通过点击该按钮来提交表单。
3. 在JSP页面中添加一个Java代码段,通过该代码段来判断input元素是否选择了文件或文件夹。
以下是示例代码:
```
<%@ page import="java.io.*" %>
<%
String fileName = "";
String savePath = "C:/uploads/"; // 文件保存路径
File file = null;
int maxSize = 10 * 1024 * 1024; // 文件最大大小,10MB
String[] allowedExt = {"jpg", "jpeg", "png", "gif", "bmp"}; // 允许上传的文件类型
String errorMsg = "";
if (request.getMethod().equalsIgnoreCase("post")) {
String contentType = request.getContentType();
if (contentType.indexOf("multipart/form-data") >= 0) {
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(maxSize);
List items = upload.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
String name = item.getName();
if (name != null && !name.equals("")) {
fileName = name.substring(name.lastIndexOf("\\") + 1);
String ext = fileName.substring(fileName.lastIndexOf(".") + 1);
boolean allowed = false;
for (String s : allowedExt) {
if (s.equals(ext.toLowerCase())) {
allowed = true;
break;
}
}
if (!allowed) {
errorMsg = "只允许上传以下类型的文件:" + Arrays.toString(allowedExt);
break;
}
file = new File(savePath + fileName);
item.write(file);
}
}
}
}
}
if (file != null && file.exists()) {
out.println("文件上传成功!");
} else {
if (errorMsg.length() > 0) {
out.println(errorMsg);
} else {
out.println("请选择要上传的文件!");
}
}
%>
<form name="uploadForm" method="post" enctype="multipart/form-data">
<input type="file" name="uploadFile">
<input type="submit" value="上传">
</form>
```
Java将微信上传的图片MultipartFile存为URL,将URL返回给微信小程序,并显示到微信小程序的<image>中渲染,给出Java和微信小程序的详细代码
Java代码
```java
@RestController
public class ImageUploadController {
private static final String UPLOAD_DIR = "/uploads/";
@Autowired
private ServletContext servletContext;
@PostMapping("/upload")
public String uploadImage(@RequestParam("file") MultipartFile file) throws IOException {
String filename = StringUtils.cleanPath(file.getOriginalFilename());
String ext = filename.substring(filename.lastIndexOf('.') + 1);
String storedFilename = UUID.randomUUID().toString() + "." + ext;
String storedDir = servletContext.getRealPath(UPLOAD_DIR);
Path storedPath = Paths.get(storedDir + storedFilename);
Files.copy(file.getInputStream(), storedPath, StandardCopyOption.REPLACE_EXISTING);
String url = "/uploads/" + storedFilename;
return url;
}
}
```
微信小程序代码
```html
<view class="container">
<form>
<button type="button" bindtap="chooseImage">选择图片</button>
<button type="button" bindtap="uploadImage">上传图片</button>
</form>
<image src="{{imageUrl}}" mode="aspectFit" />
</view>
```
```javascript
Page({
data: {
imageUrl: '',
file: null
},
chooseImage: function () {
var that = this;
wx.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success: function (res) {
that.setData({
imageUrl: res.tempFilePaths[0],
file: res.tempFiles[0]
})
}
})
},
uploadImage: function () {
var that = this;
wx.uploadFile({
url: 'http://localhost:8080/upload',
filePath: that.data.imageUrl,
name: 'file',
success: function (res) {
that.setData({
imageUrl: 'http://localhost:8080' + res.data
})
}
})
}
})
```