java - 在spring中创建上传功能,出错抛出异常[处理程序处理失败;嵌套异常是 java.lang.StackOverflowError

标签 java spring file-upload controller postman

相关文件-migrationruleserviceimpl

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import javax.inject.Named;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

@Named
public class MigrationRuleServiceImpl implements MigrationRuleService {

@Override
    @Transactional(propagation=Propagation.REQUIRED)
    public String migrationRuleUpload(MultipartFile file){

         if (!file.isEmpty()) {
                try {
                    byte[] bytes = file.getBytes();
                    BufferedOutputStream stream =
                            new BufferedOutputStream(new FileOutputStream(new File("lul")));
                    stream.write(bytes);
                    stream.close();
                    return "You successfully uploaded ";

                } catch (Exception e) {
                    return "You failed to upload  => " + e.getMessage();
                }
            } else {
                return "You failed to upload because the file was empty.";
            }
    }
}

迁移规则服务

import org.springframework.web.multipart.MultipartFile;

public interface MigrationRuleService {


    String migrationRuleUpload(MultipartFile file);

}

迁移规则 Controller

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping(value = "/upload")
public class MigrationRuleController {



    @RequestMapping(method = RequestMethod.POST)
    public String migrationRuleUpload(@RequestParam("file") MultipartFile file) {
        return migrationRuleUpload(file);
    }

}

当我在 postman 中尝试此操作时,我收到内部服务器错误 500 - 来自 tomcat 日志的错误消息:

SEVERE: Servlet.service() for servlet [dispatcher] in context with path [] threw exception [Handler processing failed; nested exception is java.lang.StackOverflowError] with root cause
java.lang.StackOverflowError at MigrationRuleController.migrationRuleUpload(MigrationRuleController.java:17)
<the previous line repeated bazilion of times>

这可能有什么问题?

最佳答案

原因很清楚 - 你递归地调用了一些函数。罪魁祸首是:

public String migrationRuleUpload(@RequestParam("file") MultipartFile file) {
    return migrationRuleUpload(file);
}

该方法只是调用自身。相反,您可能想注入(inject) MigrationRuleService 实例并在该实例上调用 migrationRuleUpload:

public class MigrationRuleController {
    @Inject
    private MigrationRuleService migrationRuleService;

    @RequestMapping(method = RequestMethod.POST)
    public String migrationRuleUpload(@RequestParam("file") MultipartFile file) {
        return migrationRuleService.migrationRuleUpload(file);
    }
}

关于java - 在spring中创建上传功能,出错抛出异常[处理程序处理失败;嵌套异常是 java.lang.StackOverflowError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38824361/

相关文章:

java - 在 Java Servlet 中上传文件

android - 上传图片错误: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getLastPathSegment()' on a null object reference

java - 比较实现 Comparable 的对象的更优雅的方式

java - 如果我先放置 ScrollView 然后布局,则布局不会显示

java - 仅以一种颜色空间分量值的灰度显示位图

java - 如何在 Spring 中将 bean 集合创建为 XML Java 配置?

docker 中 rabbitmq 的 Spring Boot 应用程序问题

java - 是否有返回请求的 Number 子类的 Java 数字字符串文字的解析器?

java - Spring + JPA/Hibernate 不保存多对多映射

php - 添加另一个具有javascript功能的文件上传字段输入消失