2 u011533296 u011533296 于 2013.08.15 09:53 提问

怎样在JSP中实现上传图片的功能?

刚实习,在工作中遇到很多问题,我想知道怎样用JSP上传附件或者图片到服务器,并且把图片名称与后缀名穿到数据库,请问我该怎么写?

1个回答

lucumu
lucumu   2013.08.15 10:37
已采纳

html

<form method="POST" enctype="multipart/form-data" action="fup.action">
  File to upload: <input type="file" name="upfile"><br/>
  Notes about the file: <input type="text" name="note"><br/>
  <br/>
  <input type="submit" value="Press"> to upload the file!
</form>

servlet

// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload();

// Parse the request
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
  FileItemStream item = iter.next();
  String name = item.getFieldName();
  InputStream stream = item.openStream();
  if (item.isFormField()) {
      System.out.println("Form field " + name + " with value "
        + Streams.asString(stream) + " detected.");
  } else {
    System.out.println("File field " + name + " with file name "
        + item.getName() + " detected.");
    // Process the input stream
    ...
  }
}

http://commons.apache.org/proper/commons-fileupload/streaming.html

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!