Java如何删除文件?

  private void deleteFiles(List fileList, String path) {

for (int index = 0; index fileList, String path, String zipFN) throws IOException {

FileOutputStream fileOut = new FileOutputStream(path + zipFN);

ZipOutputStream outputStream = new ZipOutputStream(fileOut);

for (int index = 0; index < fileList。

Java如何删除文件?

  private void deleteFiles(List fileList, String path) {

for (int index = 0; index fileList, String path, String zipFN) throws IOException {

FileOutputStream fileOut = new FileOutputStream(path + zipFN);

ZipOutputStream outputStream = new ZipOutputStream(fileOut);

for (int index = 0; index < fileList。

用java实现上传功能

下面是我用过的一段代码,fileupload、servlet搞的

DiskFileItemFactory factory = new DiskFileItemFactory();//为该请求创建一个DiskFileItemFactory对象,通过它来解析请求。执行解析后,所有的表单项目都保存在一个List中。

factory.setSizeThreshold(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD);

ServletFileUpload upload = new ServletFileUpload(factory);

upload.setSizeMax(-1);

upload.setHeaderEncoding("UTF-8");

List items;//对应jsp表单的域

File icon = null;//这是我要保存的文件,是一个icon

try {

items = upload.parseRequest(request);//通过request获得请求表单的域

if(items!=null&&items.size()>0){

Iterator itr = items.iterator();

while(itr.hasNext()){

FileItem item = (FileItem) itr.next();

if(item.isFormField()){

//普通表单域

String fieldName = item.getFieldName();//表单域的name属性

String value = item.getString("UTF-8");//表单域的value或者textarea的内容

if("news.title".equals(fieldName)){

news.setTitle(value);

}

...

}else{

//如果不是普通的表单域,即文本域

String fieldName = item.getFieldName();//文本域的name属性

String value = item.getName();//文件名

if("icon".equals(fieldName)){

if(!StringUtils.isEmpty(value)){

String filename = String.valueOf(UUID.randomUUID());

new File(WinWinConstant.file_path+File.separatorChar+WinWinConstant.NEWS).mkdirs();

//设置icon保存的路径

icon = new File(WinWinConstant.file_path+File.separatorChar+WinWinConstant.NEWS + File.separatorChar + filename+value.substring(value.lastIndexOf('.')));

item.write(icon);//保存文件

}

}

...

}

}

}catch(Exception e){

e.printStackTrace();

}我有一段上传图片的代码,并且可以根据实际,按月或按天等,生成存放图片的文件夹

首先在jsp上放一个file的标签这些我都不说了,你也一定明白,我直接把处理过程给你发过去

我把其中存到数据库中的内容删除了,你改一下就能用

* 上传图片

* @param servlet

* @param request

* @param response

* @return

* @throws exception

//这里我是同步上传的,你随意

public synchronized string importpic(httpservlet servlet, httpservletrequest request,httpservletresponse response) throws exception {

simpledateformat formatdate = new simpledateformat("yyyymm");

date nowtime=new date();

string formatnowtime=formatdate.format(nowtime);

file root = new file(request.getrealpath("/")+"uploadfile/images/"+formatnowtime+"/"); //应保证在根目录中有此目录的存在 如果没有,下面则上创建新的文件夹

if(!root.isdirectory())

system.out.println("创建新文件夹成功"+formatnowtime);

root.mkdir();

int returnflag = 0;

smartupload mysmartupload =new smartupload();

int file_size_max=1024000;

string ext="";

string url="uploadfile/images/"+formatnowtime+"/";

// 只允许上载此类文件

try{

// 初始化

mysmartupload.initialize(servlet.getservletconfig(),request,response);

mysmartupload.setallowedfileslist("jpg,gif,bmp,jpeg,png,jpg");

// 上载文件

mysmartupload.upload();

} catch (exception e){

response.sendredirect()//返回页面

com.jspsmart.upload.file myfile = mysmartupload.getfiles().getfile(0);

if (myfile.ismissing()){ //没有选择图片做提示!

returnflag = 3;

}else{

string myfilename=myfile.getfilename(); //取得上载的文件的文件名

ext= myfile.getfileext(); //取得后缀名

if(ext.equals("jpg")||ext.equals("gif")||ext.equals("bmp")||ext.equals("jpeg")||ext.equals("png")||ext.equals("jpg")){ //jpeg,png不能上传!)

int file_size=myfile.getsize(); //取得文件的大小

string saveurl="";

if(file_size

try{

//我上面说到,把操作数据库的代友删除了,这里就应该是判断,你的图片是不是已经存在了,存在要怎么处理,不存在要怎么处了,就是你的事了 }

//更改文件名,取得当前上传时间的毫秒数值

calendar calendar = calendar.getinstance();

//string filename = string.valueof(calendar.gettimeinmillis());

string did = contractbean.getmaxseq("multimedia_seq");

string filename = did;

string flag = "0";

string path = request.getrealpath("/")+url;

string ename = myfile.getfileext();

//.tolowercase()转换大小写

saveurl=request.getrealpath("/")+url;

saveurl+=filename+"."+ext; //保存路径

myfile.saveas(saveurl,mysmartupload.save_physical);

//将图片信息插入到数据库中

// ------上传完成,开始生成缩略图-----

java.io.file file = new java.io.file(saveurl); //读入刚才上传的文件

string newurl=request.getrealpath("/")+url+filename+"_min."+ext; //新的缩略图保存地址

image src = javax.imageio.imageio.read(file); //构造image对象

float tagsize=200;

int old_w=src.getwidth(null);

int old_h=src.getheight(null);

int new_w=0;

int new_h=0;

int tempsize;

float tempdouble;

if(old_w>old_h){

tempdouble=old_w/tagsize;

}else{

tempdouble=old_h/tagsize;

// new_w=math.round(old_w/tempdouble);

// new_h=math.round(old_h/tempdouble);//计算新图长宽

new_w=150;

new_h=110;//计算新图长宽

bufferedimage tag = new bufferedimage(new_w,new_h,bufferedimage.type_int_rgb);

tag.getgraphics().drawimage(src,0,0,new_w,new_h,null); //绘制缩小后的图

fileoutputstream newimage=new fileoutputstream(newurl); //输出到文件流

jpegimageencoder encoder = jpegcodec.createjpegencoder(newimage);

encoder.encode(tag); //近jpeg编码

newimage.close();

returnflag = 1;

}else{

returnflag = 0;

system.out.println("('上传文件大小不能超过"+(file_size_max/1000)+"k');");

}else{

returnflag = 2;

response.sendredirect();

return "11";

}学习smartUpload的源码。

javaweb开发中,上传一个文件。

文件上传到服务器,在服务器上会产生一个临时文件,也就是File对象,大小什么的.File类都有对应的方法. 至于视频你可以百度下 "java获得视频文件信息"应该在一开始生成随机数组的时候将它存储在session中通过页面提交后从session中得到产生的随机数组与其进行匹配,事先就把随机值存储到session中