相关文件-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/