我想上传文件,但无法从html输入类型=文件获取文件的确切路径可以请任何人帮忙。 [英] I want to upload file but unable to get exact path of file from html input type=file can anybody help please.

查看:96
本文介绍了我想上传文件,但无法从html输入类型=文件获取文件的确切路径可以请任何人帮忙。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Html代码:

 <  !DOCTYPE     html  >  
< html >
< head >
< meta charset = ISO-8859-1 >
< title > 在此处插入标题< / tit le >

< / head >
< 正文 >
< form 操作 = index method = get < span class =code-attribute> enctype = multipart / form-data >
< 输入 < span class =code-attribute> type = text name = text1 >
< ; 输入 type < span class =code-keyword> = file 名称 = img >
< input type = sub mit name = 提交 >
< / form >
< ; / body >
< / html >







JSP代码:



@WebServlet(/ index)

公共类索引扩展HttpServlet {

私有静态最终长版serialVersionUID = 1L;



/ **

*默认构造函数。

* /

公共索引(){

// TODO自动生成构造函数存根

}



/ **

* @see HttpServlet #doGet(HttpServletRequest请求,HttpServletResponse响应)

* /

protected void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException {

String source = request.getParameter(img);

系统.out.println(Path is+ source);

文件S =新文件(E:\\\\+ source);

文件D =新文件(E:\\photos);



System.out.println(你好);

尝试

{

if(S.exists())

{

if(!D.exists( ))

{

D.createNewFile();

System.out.println(新文件创建);

}

if(D.exists())

{

FileChannel SS = null;

菲尔eChannel DD = null;

SS = new FileInputStream(S).getChannel();

DD = new FileOutputStream(D).getChannel();

if(SS!= null&& DD!= null)

{

DD.transferFrom(SS,0,SS.size());

System.out.println (成功复制);



}

if(SS!= null)

{

SS.close();

}

if(DD!= null)

{

DD.close();

}



}

}

else

{

System.out.println(没有文件存在该名称);

}

}

catch(NullPointerException ne)

{

System.out.println(Error+ ne);

}

catch(例外e)

{

System.out.println(一般错误+ e);

}









< br $>




}



/ **

* @see HttpServlet #doPost(HttpServletRequest请求,HttpServletResponse响应)

* /

protected void doPost(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException {

// TODO自动生成方法stub

}



}

解决方案

我没有时间查看后端代码,但是我注意到html页面中有一个错误,那就是文件上传操作的表单方法必须是'post'而不是'get'。您应该用引号括起输入属性的值。希望能解决您的问题。

请参阅此内容以获取进一步的帮助: http://www.tutorialspoint。 com / jsp / jsp_file_uploading.htm [ ^ ]

Html Code:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>

</head>
<body>
<form Action="index" method="get" enctype="multipart/form-data">
<input type="text" name=text1>
<input type="file" name=img>
<input type="submit" name="submit">
</form>
</body>
</html>




JSP CODE:

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

/**
* Default constructor.
*/
public index() {
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String source=request.getParameter("img");
System.out.println("Path is "+source);
File S=new File("E:\\k"+source);
File D=new File("E:\\photos");

System.out.println("hello");
try
{
if(S.exists())
{
if(!D.exists())
{
D.createNewFile();
System.out.println("new file created");
}
if(D.exists())
{
FileChannel SS=null;
FileChannel DD=null;
SS=new FileInputStream(S).getChannel();
DD=new FileOutputStream(D).getChannel();
if(SS!=null && DD!=null)
{
DD.transferFrom(SS, 0, SS.size());
System.out.println("Successfully copied");

}
if(SS!=null)
{
SS.close();
}
if(DD!=null)
{
DD.close();
}

}
}
else
{
System.out.println("No file Exist with that name");
}
}
catch(NullPointerException ne)
{
System.out.println("Error"+ne);
}
catch(Exception e)
{
System.out.println("General Error"+e);
}







}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

}

解决方案

I do not have time to go through the back end code, but I noticed one mistake in the html page, that is the form method must be 'post' not 'get' for file upload operation. And you should enclose the value of input attributes with quotes. Hope that solves your problem.
Refer this for further help: http://www.tutorialspoint.com/jsp/jsp_file_uploading.htm[^]


这篇关于我想上传文件,但无法从html输入类型=文件获取文件的确切路径可以请任何人帮忙。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆