在Java Servlet中上传文件 [英] Uploading a file in Java Servlet

查看:149
本文介绍了在Java Servlet中上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是web项目的新手,我不太了解如何使用TomCat。我可以在我的一个jsp页面上传文件。由于我的项目只是本地的,我以为我可以使用一个多部分的形式,其中人会选择文件(这部分进展良好),后来从我的Servlet中检索文件路径。我不能完成这个部分,它似乎只给我的文件的名称,而不是它的整个路径。

任何人都可以指向我的方向?我已经阅读了多个关于Apache文件上传的文章,并从多部分表单中获取信息,但似乎没有任何帮助。



如何从表单获取文件路径或者我怎样才能得到上传的文件在我的Java类中使用?



在此先感谢



。 jsp:

 < form method =postaction =uploadenctype =multipart / form-data> 
< input type =filename =filePathaccept =application / vnd.ms-excel,application / vnd.openxmlformats-officedocument.spreadsheetml.sheet>< / input>
< input type =submitvalue =Enviar>< / input>
< / form>

Java Servlet:

  protected void doPost(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException 
{
PrintWriter out = response.getWriter();
out.println(< html>< body>);

尝试
{
列表< FileItem> items = new ServletFileUpload(new DiskFileItemFactory())。parseRequest(request);
for(FileItem item:items)
{
if(item.isFormField())
{
//处理常规表单字段(input type =text |收音机|复选框|等等,选择等)。
String fieldname = item.getFieldName();
String fieldvalue = item.getString();
$ b $ out.println(< h1>+ fieldname +/+ fieldvalue +< / h1>);
}
else
{
//处理表单文件字段(输入类型=文件)。
String fieldname = item.getFieldName();
String filename = item.getName();
InputStream filecontent = item.getInputStream();
String s = filecontent.toString();
out.println(< h1>+ s +/+ filename +< / h1>);
item.write(null);


$ b catch(FileUploadException e)
{
throw new ServletException(Can not parse multipart request。,e);

catch(Exception e)
{
e.printStackTrace();

$ b $ out.println(< / body>< / html>);


解决方案

不提供文件路径浏览器的安全功能。

您的代码中有可用的文件内容( InputStream filecontent ),所以您可以使用它或使用 FileItem 的简便方法,例如

  item.write(new File ( /path/to/myfile.txt)); 


I have a Java Dynamic Web Project, and I'm using TomCat v7.0.

I am new to web projects and I didn't quite understand how I can upload a file in one of my jsp pages. Since my project is intended to be only local, I thought I could use a multipart form in which the person would choose the file (and this part goes fine) and later retreive the file path from my Servlet. I can't complete this part though, it appears to only give me the name of the file, not its entire path.

Can anyone point me to the right direction? I've read several posts about Apache File Upload and retreiving information from the multipart form but nothing seems to help me.

How can I get the file path from a form or alternatively how can I get the uploaded file to use in my Java classes?

Thanks in advance.

.jsp:

<form method="post" action="upload" enctype="multipart/form-data">
<input type="file" name="filePath" accept="application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"></input>
<input type="submit" value="Enviar"></input>
</form>

Java Servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    PrintWriter out = response.getWriter();
    out.println("<html><body>");

    try
    {
        List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
        for (FileItem item : items)
        {
            if (item.isFormField())
            {
                // Process regular form field (input type="text|radio|checkbox|etc", select, etc).
                String fieldname = item.getFieldName();
                String fieldvalue = item.getString();

                out.println("<h1>"+fieldname+" / "+fieldvalue+"</h1>");
            }
            else
            {
                // Process form file field (input type="file").
                String fieldname = item.getFieldName();
                String filename = item.getName();
                InputStream filecontent = item.getInputStream();
                String s = filecontent.toString();
                out.println("<h1>"+s+" / "+filename+"</h1>");
                item.write(null);
            }
        }
    }
    catch (FileUploadException e)
    {
        throw new ServletException("Cannot parse multipart request.", e);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    out.println("</body></html>");
}

解决方案

Not providing the file path is a security feature of the browser.

You have the file contents available in your code (InputStream filecontent) so you could use that or use one of the convenience methods on FileItem, e.g.

item.write(new File("/path/to/myfile.txt"));

这篇关于在Java Servlet中上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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