【问题标题】:File upload in jspjsp文件上传
【发布时间】:2011-05-01 12:50:55
【问题描述】:

我想上传任何类型的文件(一次一个文件),但我面临的问题是我的 **html 表单只返回源文件名而不是路径。 我的html文件: 选择要上传的文件:

FileUpload.jsp :-

<%@ page import="java.util.*,java.io.*"%>

<%
String path=request.getParameter("filename");
System.out.println(path);
String newPath="";
int count=0;

if(path!=null)
{
String arr[]=new String[100];
StringTokenizer st=new StringTokenizer(path,"/");
while(st.hasMoreTokens())
{
    arr[count]=st.nextToken();
count++;
}
// create ur own path

newPath="/home/saurabh/"+arr[count-1];
int c;
try{
FileInputStream fis=new FileInputStream(path);
FileOutputStream fos=new FileOutputStream(newPath);
while((c=fis.read())!=-1)
{
fos.write((char)c);
}
}
catch(Exception e){e.printStackTrace();}
}
out.println("Thanks for using");
out.println("<br>");
out.println("<br>");
out.println("1.File1 Uploaded from :: "+path);
out.println("<br>");
out.println("<br>");
out.println("2.Uploaded File1 is Saved in :: "+newPath);
%>

【问题讨论】:

标签: eclipse jsp ubuntu-10.10 tomcat


【解决方案1】:

html 表单只返回源文件名而不是路径

您不需要知道上传文件的文件路径。

您必须在表单标签中更改/添加enctype="multipart/formdata"。这样您就可以访问文件而无需获取文件路径。

也请看apache commons fileupload

【讨论】:

    猜你喜欢
    • 2015-04-15
    • 2012-03-21
    • 2011-02-13
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    • 2013-07-09
    • 2012-01-14
    • 2013-01-11
    相关资源
    最近更新 更多