Java文件上傳到服務器

 FormFile file=manform.getFile(); 
  String fileName = null;
  String pathname=null;
  String fileAddre="/uptoService";
  try {
   InputStream stream = file.getInputStream();
    String filePath = request.getRealPath(fileAddre);
          File file1 = new File(filePath);
       ((File) file1).mkdir();   
    fileName = System.currentTimeMillis()
     + file.getFileName().substring(
       file.getFileName().lastIndexOf('.'));
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   OutputStream bos = new FileOutputStream(filePath + "/"
     + fileName);
   pathname=filePath+"/"+fileName;
   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();
  }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章