Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.util.NoSuchElementException:没有值存在于java中_Java_Spring Mvc_Servlets_Service - Fatal编程技术网

java.util.NoSuchElementException:没有值存在于java中

java.util.NoSuchElementException:没有值存在于java中,java,spring-mvc,servlets,service,Java,Spring Mvc,Servlets,Service,我正在尝试将照片上载到服务。 这是我的服务课 package com.service; import com.jcraft.jsch.*; import org.springframework.stereotype.Service; import java.io.File; import java.io.FileInputStream; @Service public class sftpGlusterFsService { private String

我正在尝试将照片上载到服务。 这是我的服务课

package com.service;

import com.jcraft.jsch.*;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;

    @Service
    public class sftpGlusterFsService {

        private String host;

        private Integer port;
        private String user;
        private String password;

        private JSch jsch;
        private Session session;
        private Channel channel;
        private ChannelSftp sftpChannel;

        //for mountpoint
        public String mkdir;

        public sftpGlusterFsService(){
            super();
        }

        public sftpGlusterFsService(String host, Integer port, String user, String password){
            this.host = host;
            this.port = port;
            this.user = user;
            this.password = password;
        }
        public String getHost(){
            return host;
        }
        public void setHost(String host){
            this.host=host;
        }

        public Integer getPort(){
            return port;
        }
        public void setPort(Integer port){
            this.port=port;
        }

        public String getUser(){
            return user;
        }
        public void setUser(String user){
            this.user=user;
        }

        public String getPassword(){
            return password;
        }
        public void setPassword(String password){
            this.password=password;
        }

        public void connectSFTP() {
            try {
                jsch = new JSch();
                session = jsch.getSession(user, host,port);
                session.setConfig("StrictHostKeyChecking", "no");
                session.setPassword(password);
                session.connect();
                channel = session.openChannel("sftp");
                channel.connect();
                sftpChannel = (ChannelSftp) channel;
            } catch (JSchException e) {
                e.printStackTrace();
            }
        }

        public void disconnectSFTP() {
            System.out.println("disconnecting");
            sftpChannel.disconnect();
            channel.disconnect();
            session.disconnect();
        }
        public boolean uploadSFTP(String fileName, String mkdir) {
            FileInputStream fis = null;
            connectSFTP();
            try {
                // Change to output directory
                sftpChannel.cd(mkdir);
                // Upload file
                File file = new File(fileName);
                fis = new FileInputStream(file);
                sftpChannel.put(fis, file.getName());
                fis.close();
                System.out.println("File uploaded successfully - " + file.getAbsolutePath());
            } catch (Exception e) {
                e.printStackTrace();
            }
            disconnectSFTP();
            return false;
        }
    }
这是我的前端代码:

<button type="submit" class="btn btn-primary" onclick="uploadFileSave()"style="margin-left: 5px; margin-bottom: 5px; width: auto;">Test</button>
这是我的控制器:

@RestController
@RequestMapping("/api")
public class sftpGlusterFsController {

    private final Logger log = LoggerFactory.getLogger(sftpGlusterFsController.class);
    //glusterFS mount point
    String mkdir ="/mnt/distvolume";
    @Inject
    UserRepository userRepository;

    sftpGlusterFsService sftp = new sftpGlusterFsService("****", **, "***", "*****");
    @RequestMapping(value = "/setSftpFile",
            method= RequestMethod.GET)
    @ResponseBody
    public GenericResponse setSftpFile(final HttpServletRequest request) {
        String fileName= RequestUtil.getBody(request);
        log.info("file name ?:",fileName); //i can see but just "file name ?: so No filename value
        log.info("with vt", userRepository.findOneByLogin(SecurityUtils.getCurrentLogin()).get().toString() + fileName);  //i cant see
        int failedCount = 0, successCount = 0;
        try {
            for (int i = 0; i < fileName.length(); i++) {
                sftp.connectSFTP();
                if(sftp.uploadSFTP((userRepository.findOneByLogin(SecurityUtils.getCurrentLogin()).get()).toString() + fileName, mkdir)){
                    successCount++;
                }else{
                    failedCount++;
                }
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }sftp.disconnectSFTP();
        GenericResponse response = new GenericResponse("Failed : " + failedCount + ",Success : " + successCount);
        response.setMessage("successCount");
        response.setError("Failed : " + failedCount);
        log.debug("sonucc", response);
        return response;
    }
}
我该怎么办?哪里有错误

注意!当RequestMethod.Put方法给出错误时:500个内部错误。
同样的:

如果你想真正有机会得到答案,你需要包括堆栈跟踪并描述你遇到的问题。堆栈跟踪错误:
org.springframework.boot.context.web.ErrorPageFilter-由于异常从请求[/api/setSftpFile]转发到错误页[请求处理失败;嵌套异常为java.util.NoSuchElementException:不存在值]org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常为java.util.NoSuchElementException:不存在值
@Todd
@RestController
@RequestMapping("/api")
public class sftpGlusterFsController {

    private final Logger log = LoggerFactory.getLogger(sftpGlusterFsController.class);
    //glusterFS mount point
    String mkdir ="/mnt/distvolume";
    @Inject
    UserRepository userRepository;

    sftpGlusterFsService sftp = new sftpGlusterFsService("****", **, "***", "*****");
    @RequestMapping(value = "/setSftpFile",
            method= RequestMethod.GET)
    @ResponseBody
    public GenericResponse setSftpFile(final HttpServletRequest request) {
        String fileName= RequestUtil.getBody(request);
        log.info("file name ?:",fileName); //i can see but just "file name ?: so No filename value
        log.info("with vt", userRepository.findOneByLogin(SecurityUtils.getCurrentLogin()).get().toString() + fileName);  //i cant see
        int failedCount = 0, successCount = 0;
        try {
            for (int i = 0; i < fileName.length(); i++) {
                sftp.connectSFTP();
                if(sftp.uploadSFTP((userRepository.findOneByLogin(SecurityUtils.getCurrentLogin()).get()).toString() + fileName, mkdir)){
                    successCount++;
                }else{
                    failedCount++;
                }
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }sftp.disconnectSFTP();
        GenericResponse response = new GenericResponse("Failed : " + failedCount + ",Success : " + successCount);
        response.setMessage("successCount");
        response.setError("Failed : " + failedCount);
        log.debug("sonucc", response);
        return response;
    }
}
org.springframework.boot.context.web.ErrorPageFilter - Forwarding to error page from request [/api/setSftpFile] due to exception [Request processing failed; nested exception is java.util.NoSuchElementException: No value present] org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.util.NoSuchElementException: No value present