----------------------------------------------------------------------------------------------------------
转自: http://blog.csdn.net/xxxx1243/archive/2009/03/30/4037439.aspx
----------------------------------------------------------------------------------------------------------
commons-fileupload-1.2.1相对于commons-fileupload-1.1做了很多改进,废弃了一些函数。
该版本的上传代码如下:
public class UploadServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String path = request.getRealPath("/");
//file upload factory
DiskFileItemFactory factory = new DiskFileItemFactory();
//setting upload file path
factory.setRepository(new File(path));
//set default memory size
factory.setSizeThreshold(1024*1024);
//
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List<FileItem> list = upload.parseRequest(request);
for (FileItem item : list) {
if(item.isFormField()){
String name = item.getFieldName();//input name
String value = item.getName();//input content
request.setAttribute(name, value);
}else{
String name = item.getFieldName();//input name
String value = item.getName();//input content
value = value.substring(value.lastIndexOf("//")+1,value.length());
//1.--------------
//output file
OutputStream fileOutStream = new FileOutputStream(new File(path,value));
//input file
InputStream fileInputStream = item.getInputStream();
//file buffer
byte [] buffer = new byte[1024];
//read
int length = 0;
while(( length = fileInputStream.read(buffer)) > 0){
fileOutStream.write(buffer,0,length);
}
//close
fileInputStream.close();
fileOutStream.close();
//2.--------------
item.write(new File(path,value));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
分享到:
相关推荐
commons-fileupload-1.2.1.jar,commons-io-1.4.jar本人正在使用的
包含commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar 下载即可使用
commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar jar 文件。 commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar 案例上传: http://hi.baidu.com/lichao77821/blog commons-fileupload-1.2.1.jar和commons-...
commons-fileupload-1.2.1.jar和commons-io-1.4.jar包
java实现文件保存,接收所需的jar包,(commons-fileupload-1.2.1.jar,commons-io-2.0.1.jar),亲测可用
用servlet技术实现文件上传下载我们需要导入两个额外的jar包,commons-io-1.4.jar和commons-fileupload-1.2.1.jar,将这个两个jar 包导入WEB-INF/lib目录里
commons-fileupload-1.2.1.jar commons-io-1.4.jar
commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar 案例上传: http://hi.baidu.com/lichao77821/blog
springmvc上传文件必备的两个jar包-commons.fileupload-1.2.1.jar和commons.io-1.4.0.jar,导入即可
用开源的FileUpload组件,可以很方便的给自己的系统添加功能强大的文件上传模块。 该资源文件是两个jar打包成的压缩文件。
Apache的commons-fileupload.jar可方便的实现文件的上传功能
struts2上传文件需要的jar包 commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar
spring上传文件jar包括commons-fileupload-1.2.1.jar和commons-io-2.0.jar。 官方版本,放心使用。
struts2中用于文件上传的两个jar包,非常实用。
gwt文件上传下载架包commons-fileupload-1.2.1.jar、gwtextux.jar、commons-io-1.4.jar
commons-fileupload-1.2.1.jar,commons-io-1.4.jarcommons-io-1.4.jar
struts,springMVC 上传文件需要的jar包 commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar