「java文件上传与下载」java 上传下载文件

博主:adminadmin 2022-11-24 03:07:06 66

本篇文章给大家谈谈java文件上传与下载,以及java 上传下载文件对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

JAVA 上传下载文件

Java代码实现文件上传

  FormFile file=manform.getFile(); 

  String newfileName = null;

  String newpathname=null;

  String fileAddre="/numUp";

  try {

   InputStream stream = file.getInputStream();// 把文件读入

    String filePath = request.getRealPath(fileAddre);//取系统当前路径

          File file1 = new File(filePath);//添加了自动创建目录的功能

       ((File) file1).mkdir();   

    newfileName = System.currentTimeMillis()

     + file.getFileName().substring(

       file.getFileName().lastIndexOf('.'));

   ByteArrayOutputStream baos = new ByteArrayOutputStream();

   OutputStream bos = new FileOutputStream(filePath + "/"

     + newfileName);

   newpathname=filePath+"/"+newfileName;

   System.out.println(newpathname);

   // 建立一个上传文件的输出流

    System.out.println(filePath+"/"+file.getFileName());

   int bytesRead = 0;

   byte[] buffer = new byte[8192];

   while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {

    bos.write(buffer, 0, bytesRead);// 将文件写入服务器

   }

   bos.close();

   stream.close();

    } catch (FileNotFoundException e) {

   e.printStackTrace();

  } catch (IOException e) {

   e.printStackTrace();

  }

java如何实现文件上传和下载的功能

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.*;

import net.sf.json.JSONObject;

import action.StudentAction;

public class UploadServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

this.doPost(request, response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

boolean result=true;

SmartUpload mySmartUpload=new SmartUpload();

mySmartUpload.initialize(this.getServletConfig(), request,response);

mySmartUpload.setTotalMaxFileSize(50*1024*1024);//大小限制

mySmartUpload.setAllowedFilesList("doc,docx");//后缀名限制

try {

mySmartUpload.upload();

com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);

myFile.saveAs("/file/"+1+".doc");//保存目录

} catch (SmartUploadException e) {

e.printStackTrace();result=false;

}

//*****************************//

response.setContentType("text/html;charset=UTF-8");

response.setHeader("Cache-Control","no-cache");

PrintWriter out = response.getWriter();

out.print(result);

out.flush();

out.close();

}

}

//我这是ajax方式的,不想这样,把//**********************//以下部分修改就行了

//需要SmartUpload组件,去网上下个就行了,也有介绍的

Java 批量大文件上传下载如何实现?

解决这种大文件上传不太可能用web上传的方式,只有自己开发插件或是当门客户端上传,或者用现有的ftp等。

1)开发一个web插件。用于上传文件。

2)开发一个FTP工具,不用web上传。

3)用现有的FTP工具。

下面是几款不错的插件,你可以试试:

1)Jquery的uploadify插件。具体使用。你可以看帮助文档。

关于java文件上传与下载和java 上传下载文件的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-24,除非注明,否则均为首码项目网原创文章,转载请注明出处。