org.springframework.http.MediaType.parseMediaType
时间: 2023-07-21 16:27:44 浏览: 52
这个错误是因为在使用Spring框架中org.springframework.http.MediaType.parseMediaType方法时,传入的参数为空字符串或null导致的。这个方法用于将字符串解析为MediaType类型,如果传入的参数为空,则会抛出IllegalArgumentException异常。
解决方法是在调用parseMediaType方法之前,先判断传入的参数是否为空,如果为空则不调用该方法。示例代码如下:
```
String mediaTypeStr = "application/json";
if(StringUtils.isNotBlank(mediaTypeStr)){
MediaType mediaType = MediaType.parseMediaType(mediaTypeStr);
// do something
}
```
在这个示例中,我们使用了Apache Commons Lang库中的StringUtils.isNotBlank方法判断传入的字符串是否为空或null。如果不为空,则调用parseMediaType方法将字符串解析为MediaType类型。
相关问题
使用spring boot写图片的上传下载
要使用Spring Boot来实现图片的上传下载,可以使用Spring Boot提供的Multipart文件上传功能和Resource资源访问功能。
首先,在pom.xml文件中添加以下依赖:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
```
然后,在application.properties文件中添加以下配置:
```
# 储存位置
upload.dir=/path/to/upload/dir
```
接下来,创建一个Controller类来处理上传和下载请求:
```
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@Controller
public class FileController {
private static final String UPLOAD_DIR = "/path/to/upload/dir";
@PostMapping("/upload")
public ResponseEntity<String> handleFileUpload(@RequestParam("file") MultipartFile file) {
String filename = StringUtils.cleanPath(file.getOriginalFilename());
try {
if (file.isEmpty()) {
throw new RuntimeException("文件为空");
}
if (filename.contains("..")) {
throw new RuntimeException("无效的文件名");
}
try (InputStream inputStream = file.getInputStream()) {
Files.copy(inputStream, Paths.get(UPLOAD_DIR).resolve(filename));
}
} catch (IOException e) {
throw new RuntimeException("上传失败");
}
return ResponseEntity.ok("上传成功");
}
@GetMapping("/download")
public ResponseEntity<Resource> serveFile(@RequestParam("filename") String filename) {
Path file = Paths.get(UPLOAD_DIR).resolve(filename);
Resource resource = null;
try {
resource = new InputStreamResource(Files.newInputStream(file));
} catch (IOException e) {
throw new RuntimeException("文件不存在");
}
return ResponseEntity.ok()
.contentLength(file.toFile().length())
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(resource);
}
}
```
在上面的Controller类中,我们使用了MultipartFile来接收上传的文件,并使用Files.copy方法将文件复制到指定的目录下。在下载文件时,我们使用了Resource和InputStreamResource来读取文件内容,并将其包装成ResponseEntity对象返回给客户端。
最后,启动应用程序,可以使用以下命令上传文件:
```
curl -F "file=@/path/to/file.jpg" http://localhost:8080/upload
```
也可以使用以下命令下载文件:
```
curl -OJ http://localhost:8080/download?filename=file.jpg
```
其中,-O选项表示将文件保存到本地,-J选项表示使用文件名作为保存文件的名称。
Java MultipartFile 下载本地文件
您好!要使用Java的MultipartFile类下载本地文件,您可以按照以下步骤进行操作:
1. 首先,确保您已经引入了相关的依赖。在您的项目中,您可能需要使用Spring框架或者其他的文件上传库来处理MultipartFile对象。
2. 创建一个方法来处理文件下载。您可以使用HttpServletResponse对象将文件发送给客户端。以下是一个示例代码:
```java
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/file")
public class FileController {
@Autowired
private FileStorageService fileStorageService;
@GetMapping("/download/{fileName}")
public ResponseEntity<Resource> downloadFile(@PathVariable String fileName, HttpServletRequest request) {
// 加载文件资源
Resource resource = fileStorageService.loadFileAsResource(fileName);
// 确定文件的MIME类型
String contentType = null;
try {
contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
} catch (IOException ex) {
// 处理获取MIME类型的异常情况
}
// 如果无法确定MIME类型,则设置为默认类型
if (contentType == null) {
contentType = "application/octet-stream";
}
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(contentType))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
.body(resource);
}
}
```
在上述示例中,`FileStorageService`是一个自定义的文件存储服务,用于加载文件资源。
3. 在您的应用程序中,您可以通过提供文件的路径或者其他标识来调用该下载方法。例如,您可以创建一个RESTful API端点,当客户端请求下载文件时,将文件名作为路径参数传递给该端点。
以上方法将会返回一个ResponseEntity对象,其中包含了文件资源以及一些响应头部信息。在客户端收到响应后,将会触发文件下载。
请注意,以上代码示例仅供参考,并且基于Spring框架的MultipartFile类进行了假设。实际的实现可能因您的项目结构和需求而有所不同。