我正在https://spring.io/ Spring Boot 上进行一些练习.
做示例https://spring.io/guides/gs/uploading-files/,当我使用Java 8时它工作正常,但遗憾的是我在Web服务中包装的代码需要Java 7.我列出了所有的错误代码,有人可以帮助我将lambda转换为1.7兼容代码并替换新库(java.util.stream.Stream和java.util.stream.Collectors).
Application.java
@SpringBootApplication
@EnableConfigurationProperties(StorageProperties.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
CommandLineRunner init(StorageService storageService) {
return (args) -> {
storageService.deleteAll();
storageService.init();
};
}
}
Run Code Online (Sandbox Code Playgroud)
return(args) - >错误说"使用source -8或更高版本来启用lambda表达式"
FileUploadController.java
import java.util.stream.Collectors
//..
@GetMapping("/")
public String listUploadedFiles(Model model) throws IOException {
model.addAttribute("files", storageService
.loadAll()
.map(path ->
MvcUriComponentsBuilder
.fromMethodName(FileUploadController.class, "serveFile", path.getFileName().toString())
.build().toString())
.collect(Collectors.toList()));
return "uploadForm";
}
Run Code Online (Sandbox Code Playgroud)
包java.util.stream不存在
type loadAll()是错误的
错误说"使用source -8或更高版本来启用lambda表达式"
FileSystemStorageService.java
@Override
public Stream<Path> loadAll() …Run Code Online (Sandbox Code Playgroud) 假设我有一个User对象列表,其中包含两个属性... ID和Name
List<User> lst = List<User>();
Run Code Online (Sandbox Code Playgroud)
我填写了一堆用户.好的,现在我想使用RemoveAll()和此函数修剪我的列表.
private Boolean IsExisting(int id) {
//blah blah
return true;
//blah blah
return false;
}
Run Code Online (Sandbox Code Playgroud)
所以我用这句话:
gdvFoo.DataSource = lst.RemoveAll(t => IsExisting(t.ID));
Run Code Online (Sandbox Code Playgroud)
我的理解是,每当IsExisting返回true时,该元素应该从lst中删除,但是会发生什么,奇怪的是,它返回一个整数?,而不是截断的列表,我收到以下错误消息:
数据源是无效类型.它必须是IListSource,IEnumerable或IDataSource.>
public struct Speakers
{
//...
public bool BackCenter { get; set; }
public bool BackLeft { get; set; }
public bool BackRight { get; set; }
public bool FrontCenter { get; set; }
public bool FrontLeft { get; set; }
public bool FrontLeftOfCenter { get; set; }
public bool FrontRight { get; set; }
public bool FrontRightOfCenter { get; set; }
public bool SideLeft { get; set; }
public bool SideRight { get; set; }
public bool Subwoofer { get; …Run Code Online (Sandbox Code Playgroud) 我正在使用命名管道与进程通信.我已经能够使用以下代码.(原始代码在这里:via archive.org)
class ProgramPipeTest
{
public void ThreadSenderStartClient(object obj)
{
// Ensure that we only start the client after the server has created the pipe
ManualResetEvent SyncClientServer = (ManualResetEvent)obj;
using (NamedPipeClientStream pipeStream = new NamedPipeClientStream(".","ToSrvPipe",PipeDirection.Out,PipeOptions.None))
{
// The connect function will indefinately wait for the pipe to become available
// If that is not acceptable specify a maximum waiting time (in ms)
pipeStream.Connect();
Console.WriteLine("[Client] Pipe connection established");
using (StreamWriter sw = new StreamWriter(pipeStream))
{
sw.AutoFlush = true;
string …Run Code Online (Sandbox Code Playgroud)