Java JSP Servlet 上传图片文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21906899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:21:27  来源:igfitidea点击:

JSP Servlet Upload Image File

javajspservletsfile-upload

提问by Paradoxianus

How can I upload an image using simply JSP and Servlet. I used as below code to get an image in JSP page:

如何仅使用 JSP 和 Servlet 上传图像。我使用以下代码在 JSP 页面中获取图像:

<p>File: <input type="file" name="file_to_upload"> </p>

Now I would like to know how to get these files and how to save them in specific directory ? I named that folder "images", and I would like to put all files these I uploaded in that folder. The images would actually be saved when I restart my server or refresh my page.

现在我想知道如何获取这些文件以及如何将它们保存在特定目录中?我将该文件夹命名为“images”,我想将我上传的所有文件放在该文件夹中。当我重新启动我的服务器或刷新我的页面时,图像实际上会被保存。

How can I achieve this?

我怎样才能做到这一点?

回答by Leo

To upload a file using JSP, you have to deal with a multipart/form-data request.

要使用 JSP 上传文件,您必须处理 multipart/form-data 请求。

The easiest way I know to do that is using apache commons fileupload as described in details here http://www.javacodegeeks.com/2013/08/file-upload-example-in-servlet-and-jsp.html

我所知道的最简单的方法是使用 apache commons fileupload,详细信息在这里http://www.javacodegeeks.com/2013/08/file-upload-example-in-servlet-and-jsp.html

ps. the tutorial is a little bit wrong :-)

附:教程有点错误:-)

the index.jsp must contain something like this

index.jsp 必须包含这样的内容

<form action="upload" method="post" name="uploadForm" enctype="multipart/form-data">
<input name="uploadfile" type="file" size="50″>
<input name="submit" type="submit" value="Submit">
</form>

回答by cigno5.5

I think that the best way to deal with file upload (and multipart/form-data requests) is using Apache common file upload

我认为处理文件上传(和 multipart/form-data 请求)的最好方法是使用Apache 公共文件上传

It offers many useful classes to help in managing file upload matters.

它提供了许多有用的类来帮助管理文件上传问题。

回答by ying

the form which used upload file must hava ' enctype="multipart/form-data" '.

使用上传文件的表单必须包含'enctype="multipart/form-data"'。

<%@ page language="java"  pageEncoding="gb2312"%>

<%@ page import="java.io.*,java.awt.Image,java.awt.image.*,com.sun.image.codec.jpeg.*,java.sql.*,com.jspsmart.upload.*,java.util.*"%>

<%@ page import="mainClass.*" %>

<html>
  <head>
   <title>My JSP 'uploadimage.jsp' starting page</title>
  </head>
  <body>

    <%

    SmartUpload sma=new SmartUpload();
    long file_max_size=4000000;
    String filename1="",ext="",testvar="";
    String url="uploadfiles/";
    sma.initialize(pageContext);
     try

    {

    sma.setAllowedFilesList("jpg,gif");
    sma.upload();
    }catch(Exception e){

    %>

    <script language="jscript">

    alert("only jpg,gif allowed to upload!")

    window.location.href="upfile.jsp"

    </script>

    <%



     }

        try{

        com.jspsmart.upload.File myf=sma.getFiles().getFile(0);

        }else{
        ext=myf.getFileExt();

        int file_size=myf.getSize();
        String saveurl="";
        if(file_size < file_max_size){
        Calendar cal=Calendar.getInstance();
        String filename=String.valueOf(cal.getTimeInMillis());
        saveurl=request.getRealPath("/")+url;
        saveurl+=filename+"."+ext;
        myf.saveAs(saveurl,sma.SAVE_PHYSICAL

);
    myclass mc=new myclass(request.getRealPath("data/data.mdb"));
      mc.executeInsert("insert into [path] values('uploadfiles/"+filename+"."+ext+"')");    out.println("ok!");
    response.sendRedirect("showimg.jsp");
    }
    }

    }catch(Exception e){

    e.printStackTrace();

    }
    %>
  </body>
</html>

when you want to display you file ,you should write your filepath in your project ,so don't save your file at other place,it will make trouble when your server want to find it.

当你想显示你的文件时,你应该在你的项目中写你的文件路径,所以不要把你的文件保存在其他地方,当你的服务器想要找到它时会很麻烦。

first answer question ,so you understand...

第一个回答问题,所以你明白...