gistfile1.txt import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;/** * 将本地文件上传到远程服务器的共享目录 * @author panbb */public cl
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; /** * 将本地文件上传到远程服务器的共享目录 * @author panbb */ public class FileUpload { public static void main(String[] args) throws Exception { File srcPath = new File("F:\\template\\bar.png"); File parentDir = new File("\\\\192.168.1.158\\shareFile"); String fileName = System.currentTimeMillis() + ".png"; doUpload(srcPath, parentDir, fileName); System.out.println("上传成功!!"); } /** * 将本地文件上传到远程服务器的共享目录 * @param file 本地要上传的文件 * @param targetFile 远程服务器的共享目录 * @param fileName 上传后的文件名称 */ public static void doUpload(File file, File targetFile, String fileName) { try { File targetPath = new File(targetFile, fileName); InputStream in = new FileInputStream(file); OutputStream out = new FileOutputStream(targetPath); try { byte[] bs = new byte[1024]; int len = -1; while((len = in.read(bs)) != -1) { out.write(bs, 0, len); } } finally { try { out.close(); } catch (Exception e) { e.printStackTrace(); } try { in.close(); } catch (Exception e) { e.printStackTrace(); } } } catch (Exception e) { e.printStackTrace(); } } }