繁体   English   中英

上载文件使用commons-fileupload

[英]Upload file use commons-fileupload

我使用ajax上传文件。 用ajax发送到图像的链接到servlet。 而且我用FileItem来写文件。 代码jsp:

<input type="file" name="file" id="pic1" />
<input type="file" name="file" id="pic2" />
var dataString='pic1='+$("#pic1").val()+'&pic2='+$("#pic2").val();

代码servlet:

private final String UPLOAD_DIRECTORY = "C:\\img";

 String pic1= request.getParameter("pic1");
         String pic2= request.getParameter("pic2");

         String name1= new File(pic1).getName();
         FileItem fi = null;
         String name2= new File(pic2).getName();
        try {
            fi.write( new File(UPLOAD_DIRECTORY + File.separator + name1));
            fi.write( new File(UPLOAD_DIRECTORY + File.separator + name2));
            response.setContentType("text/plain");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write("OK");
        } catch (Exception ex) {
            Logger.getLogger(upLoadFileNew.class.getName()).log(Level.SEVERE, null, ex);
        }

当我将FileItem fi= null设置FileItem fi= null fi.write( new File(UPLOAD_DIRECTORY + File.separator + name1)); 不行。 我想在private final String UPLOAD_DIRECTORY = "C:\\\\img";写入文件private final String UPLOAD_DIRECTORY = "C:\\\\img"; 来人帮帮我!

你不能使用

     request.getParameter() ;

要获取文件对象,它仅返回String而不是文件对象。 您可以使用jquery ajaxfileupload发送文件对象。

http://www.simplecodestuffs.com/ajax-style-file-upload-in-java-web-application-using-jquery/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM