public static String uploadFile(MultipartFile file) {
String fileName = file.getOriginalFilename();
String name = fileName.substring(0, fileName.lastIndexOf("."));
String extension = fileName.substring(fileName.lastIndexOf("."));
String filePath = System.getProperty("user.dir") + "\\whnbgd\\";
File targetFile = new File(filePath);
if (file.isEmpty())
return fileName + "文件为空";
if (!targetFile.exists()) {
targetFile.mkdirs();
}
BufferedOutputStream stream = null;
String nowTime = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
try {
stream = new BufferedOutputStream(new FileOutputStream(filePath + name + nowTime + extension));
stream.write(file.getBytes());
stream.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (stream != null)
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return fileName + "上传成功";
}
public static void downloadNet(String urlStr, String wjcfdz, HttpServletResponse response) throws MalformedURLException {
int bytesum = 0;
int byteread = 0;
URL url = new URL(urlStr);
try {
URLConnection conn = url.openConnection();
InputStream inStream = conn.getInputStream();
FileOutputStream fs = new FileOutputStream(wjcfdz);
byte[] buffer = new byte[1204];
int length;
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread;
fs.write(buffer, 0, byteread);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}