我正在尝试将照片上传到服务中。 这是我的服务类。
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>
和uploadFileSave方法qith javascriptcode:
var filename;
function uploadFileSave(){
var fullPath = document.getElementById('photo').value;
if (fullPath) {
var startIndex = (fullPath.indexOf('\\') >= 0 ? fullPath.lastIndexOf('\\') : fullPath.lastIndexOf('/'));
filename = fullPath.substring(startIndex);
if (filename.indexOf('\\') === 0 || filename.indexOf('/') === 0) {
filename = filename.substring(1);
$.ajax({
url:"http://localhost:8080/api/setSftpFile",
path:"/api/setSftpFile",
data:filename
});
console.log("file name iss:"+filename) //i can see
}
}
};
这是我的控制者:
@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
我该怎么办?哪里有错误?
注意!当RequestMethod.Put方法给出错误时:500内部错误。 相同:https://stackoverflow.com/questions/34515859/how-to-fix-java-util-nosuchelementexception-in-java?noredirect=1#comment56776907_34515859