Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.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/9/loops/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 使用servlet jsp将文件从窗口上载到linux服务器_Java_Linux_Servlets_File Upload_Tomcat7 - Fatal编程技术网

Java 使用servlet jsp将文件从窗口上载到linux服务器

Java 使用servlet jsp将文件从窗口上载到linux服务器,java,linux,servlets,file-upload,tomcat7,Java,Linux,Servlets,File Upload,Tomcat7,我在linux机器上安装了tomcat服务器。我的webapp部署在linux机器上,具有浏览文件的功能 现在,我的要求是使用servlet将window machine的浏览文件上传到linux服务器上的指定文件夹中 我正在使用sshxcute framework运行自定义命令。此框架提供将文件上载到linux服务器机器的功能。我试着在主类中运行它,它工作正常 但是当我尝试使用servlet jsp做同样的事情时,它不起作用,因为在浏览时,我只得到文件名,而不是文件的完整路径 即使我将文件路径

我在linux机器上安装了tomcat服务器。我的webapp部署在linux机器上,具有浏览文件的功能

现在,我的要求是使用
servlet
将window machine的浏览文件上传到linux服务器上的指定文件夹中

我正在使用
sshxcute framework
运行自定义命令。此框架提供将文件上载到linux服务器机器的功能。我试着在主类中运行它,它工作正常

但是当我尝试使用servlet jsp做同样的事情时,它不起作用,因为在浏览时,我只得到文件名,而不是文件的完整路径

即使我将文件路径硬编码为ssh.uploadSingleDataToServer(“C:/Users/MyPC/Documents/Youcam/apg.doc”,“home/cloudera/Desktop/Input”)在Servlet类上,访问我的webapp到不同的窗口机器,它上载给定文件夹中的文件apg.doc,但apg.doc的数据不来为什么?请告诉我这个方法哪里不对,或者建议我不同的方法谢谢

代码通过主类工作罚款

public class TestUpload{

public void upload(){
// Initialize a SSHExec instance without referring any object. 
SSHExec ssh = null;
// Wrap the whole execution jobs into try-catch block   
try {
    // Initialize a ConnBean object, parameter list is ip, username, password
    ConnBean cb = new ConnBean("9.125.71.115", "cloudera","cloudera");
    // Put the ConnBean instance as parameter for SSHExec static method getInstance(ConnBean) to retrieve a real SSHExec instance
    ssh = SSHExec.getInstance(cb);              

    // Create a ExecShellScript, the reference class must be CustomTask
    CustomTask ct2 = new ExecShellScript("/home/cloudera/Desktop","./test.sh");
    // Connect to server
    ssh.connect();
    // Upload sshxcute_test.sh to /home/cloudera/Desktop/Input on remote system
    ssh.uploadSingleDataToServer("C:/Users/MyPC/Documents/Youcam/apg.doc", "/home/cloudera/Desktop/Input");
    // Execute task and get the returned Result object
    Result res = ssh.exec(ct2);
    // Check result and print out messages.
    if (res.isSuccess)
    {
        System.out.println("Return code: " + res.rc);
        System.out.println("sysout: " + res.sysout);
    }
    else
    {
        System.out.println("Return code: " + res.rc);
        System.out.println("error message: " + res.error_msg);
    }
} 
catch (TaskExecFailException e) 
{
    System.out.println(e.getMessage());
    e.printStackTrace();
} 
catch (Exception e) 
{
    System.out.println(e.getMessage());
    e.printStackTrace();
} 
finally 
{
    ssh.disconnect();   
}
}
public static void main(String args[])
{
TestUpload te=new TestUpload();
te.upload();
}

}
这是我从servlet尝试的,但不工作

@WebServlet("/ImportController")
@MultipartConfig
public class ImportController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        // Create path components to save the file
        final String path = request.getParameter("destination");
        final Part filePart = request.getPart("file");
        final String fileName = getFileName(filePart);

        //here i need the full path of file and file name but not sure wether sshUpload function will work

        sshUpload(fileName);

    }

    private String getFileName(final Part part) {
        final String partHeader = part.getHeader("content-disposition");
            for (String content : part.getHeader("content-disposition").split(";")) {
            if (content.trim().startsWith("filename")) {
                return content.substring(
                        content.indexOf('=') + 1).trim().replace("\"", "");
            }
        }
        return null;

    public void sshUpload(String filepath)
    {
        // Initialize a SSHExec instance without referring any object. 
        SSHExec ssh = null;
        // Wrap the whole execution jobs into try-catch block   
        try {
            // Initialize a ConnBean object, parameter list is ip, username, password
            ConnBean cb = new ConnBean("9.125.71.115", "cloudera","cloudera");
            // Put the ConnBean instance as parameter for SSHExec static method getInstance(ConnBean) to retrieve a real SSHExec instance
            ssh = SSHExec.getInstance(cb);              

            ssh.connect();
            // Upload sshxcute_test.sh to /home/tsadmin on remote system
            ssh.uploadSingleDataToServer(filepath, "/home/cloudera/Desktop/Input");
            // Execute task

        catch (Exception e) 
        {
            System.out.println(e.getMessage());
            e.printStackTrace();
        } 
        finally 
        {
            ssh.disconnect();   
        }
}
这是我的jsp

<html lang="en">
    <head>
        <title>File Upload</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <form method="POST" action="upload" enctype="multipart/form-data" >
            File:
            <input type="file" name="file" id="file" /> <br/>
            Destination:
            <input type="text" value="/tmp" name="destination"/>
            </br>
            <input type="submit" value="Upload" name="upload" id="upload" />
        </form>
    </body>
</html>

文件上传
文件:

目的地: