• 大小: 250KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-21
  • 语言: Java
  • 标签: swfupload  java版  

资源简介

网上找的一个例子,开始不能用,改后学习完,就共享出来大家学习

资源截图

代码片段和文件信息

package org.demo.tomcat;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.FileCleanerCleanup;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileCleaningTracker;

/**
 * 
 * @author  
 * @date    2010-12-16
 * @file    org.demo.tomcat.UploadServlet.java
 */
public class UploadServlet extends HttpServlet {

/**
 * 
 */
private static final long serialVersionUID = 1L;

@Override
protected void doGet(HttpServletRequest req HttpServletResponse resp)
throws ServletException IOException {
this.doPost(req resp);
}

@SuppressWarnings(“unchecked“)
@Override
protected void doPost(HttpServletRequest req HttpServletResponse resp)
throws ServletException IOException {

req.setCharacterEncoding(“utf-8“);
resp.setCharacterEncoding(“utf-8“);

boolean isMultipart = ServletFileUpload.isMultipartContent(req);
if (!isMultipart){
System.out.println(“>> This wasn‘t a file upload request!“);
return;
}

PrintWriter out = resp.getWriter();

// create factory and file cleanup tracker
FileCleaningTracker tracker = FileCleanerCleanup.getFileCleaningTracker(getServletContext());
File tmpDir = new File(getbaseDir() + “/upload/temp“);
if (!tmpDir.exists())
tmpDir.mkdirs();  //建立文件夹
DiskFileItemFactory factory = new DiskFileItemFactory(
DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD
tmpDir);
factory.setFileCleaningTracker(tracker);

// save upload file to disk
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List items = upload.parseRequest(req);
String fileName = null;
File savefile = null;
for (FileItem item : items){
if (!item.isFormField()){
// 确定是文件而不是一个普通的表单字段
fileName = item.getName();
savefile = new File(getbaseDir() + “/upload/“ + fileName);
item.write(savefile);
System.out.println(“>> [save] “ + savefile.getAbsolutePath());

// to client info
out.print(“fileId=“ + savefile.getAbsolutePath());
out.flush();
}
}
} catch (Exception e) {
System.out.println(“>> “ + e.getMessage());
throw new IOException(e.getMessage());
}
}

/**
 * Return the WEB-INF directory.
 * @return
 */
private String getbaseDir(){
return this.getServletContext().getRealPath(“/WEB-INF“);
}
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2012-10-19 17:25  SWFTest\
     文件         676  2012-10-19 17:25  SWFTest\.classpath
     文件        1040  2010-12-17 19:17  SWFTest\.project
     目录           0  2012-10-19 17:25  SWFTest\.settings\
     文件         454  2010-12-17 19:17  SWFTest\.settings\.jsdtscope
     文件         330  2010-12-17 19:17  SWFTest\.settings\org.eclipse.jdt.core.prefs
     文件         451  2010-12-17 19:17  SWFTest\.settings\org.eclipse.wst.common.component
     文件         229  2010-12-17 19:17  SWFTest\.settings\org.eclipse.wst.common.project.facet.core.xml
     文件          49  2010-12-17 19:17  SWFTest\.settings\org.eclipse.wst.jsdt.ui.superType.container
     文件           6  2010-12-17 19:17  SWFTest\.settings\org.eclipse.wst.jsdt.ui.superType.name
     目录           0  2012-10-19 17:25  SWFTest\WebContent\
     目录           0  2012-10-19 17:25  SWFTest\WebContent\meta-INF\
     文件          39  2010-12-17 19:17  SWFTest\WebContent\meta-INF\MANIFEST.MF
     目录           0  2012-10-19 17:25  SWFTest\WebContent\WEB-INF\
     目录           0  2012-11-05 09:28  SWFTest\WebContent\WEB-INF\classes\
     目录           0  2012-11-05 09:28  SWFTest\WebContent\WEB-INF\classes\org\
     目录           0  2012-11-05 09:28  SWFTest\WebContent\WEB-INF\classes\org\demo\
     目录           0  2012-11-05 09:28  SWFTest\WebContent\WEB-INF\classes\org\demo\tomcat\
     文件        3996  2012-11-05 09:28  SWFTest\WebContent\WEB-INF\classes\org\demo\tomcat\UploadServlet.class
     目录           0  2012-10-19 17:25  SWFTest\WebContent\WEB-INF\lib\
     文件       59590  2010-12-17 19:17  SWFTest\WebContent\WEB-INF\lib\commons-fileupload-1.2.2.jar
     文件      159235  2010-12-17 19:17  SWFTest\WebContent\WEB-INF\lib\commons-io-2.0.jar
     目录           0  2012-10-19 17:25  SWFTest\WebContent\WEB-INF\upload\
     目录           0  2012-11-08 17:57  SWFTest\WebContent\WEB-INF\upload\temp\
     文件         851  2010-12-17 19:17  SWFTest\WebContent\WEB-INF\web.xml
     目录           0  2012-10-19 17:25  SWFTest\WebContent\flash\
     文件       12787  2010-12-17 19:17  SWFTest\WebContent\flash\swfupload.swf
     目录           0  2012-10-19 17:25  SWFTest\WebContent\image\
     文件        3191  2010-12-17 19:17  SWFTest\WebContent\image\btnBackground.png
     文件        1855  2010-12-17 19:17  SWFTest\WebContent\image\btnUpload.png
     文件         549  2010-12-17 19:17  SWFTest\WebContent\index.jsp
............此处省略23个文件信息

评论

共有 条评论