Solution vue upload folder

 Last week, encountered such a problem, customers to upload high-definition video (1G above) when upload failed. Initially I thought it was a session expires or file size limitations by the system, resulting in errors. Check the system configuration files do not see the file size limit, web.xml in seesiontimeout is 30, I put it into a 120. But that does not work, sometimes 10 minutes to break up. Colleagues say, may be a client server network here fluctuations caused by network connection is lost, I feel a little sense. But I found also upload failed when local test, the network reasons for exclusion. Saw logs, with error: java.lang.OutOfMemoryError Java heap space upload file code is as follows: (! File.isEmpty ()) public static String uploadSingleFile (String path, MultipartFile file) {if {byte [] bytes; try {bytes = file.getBytes (); // Create the file on server File serverFile = createServerFile (path, file.getOriginalFilename ()); BufferedOutputStream stream = new BufferedOutputStream (new FileOutputStream (serverFile)); stream.write (bytes); stream.flush (); stream.close (); logger.info ( "Server File Location =" + serverFile.getAbsolutePath ()); return getRelativePathFromUploadDir (serverFile) .replaceAll ( "\\\\", "/"); } catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace (); System.out.println (e.getMessage ());}} else {System.out.println ( "file is empty." );} return null;} at first glance, no big problem, I stream.write (bytes); did not even go sentence plus a breakpoint and found. Instead bytes = file.getBytes (); On the error. The reason should be the file is too large, the number of bytes exceeds the maximum Integer (Bytes [] array), the problems caused. In this case, the file can be a little bit of time coming. Upload the modified code is as follows: public static String uploadSingleFile (String path, MultipartFile file) {if {// byte [] bytes; try {// bytes = file.getBytes (); // Create the (file.isEmpty ()!) file on server File serverFile = createServerFile (path, file.getOriginalFilename ()); BufferedOutputStream stream = new BufferedOutputStream (new FileOutputStream (serverFile)); int length = 0; byte [] buffer = new byte [1024]; inputStream inputStream = file .getInputStream (); while ((length = inputStream.read (buffer))!



详细代码可以参考一下这篇文章:http://blog.ncmem.com/wordpress/2019/08/09/java%e5%a4%a7%e6%96%87%e4%bb%b6%e4%b8%8a%e4%bc%a0/


Guess you like

Origin www.cnblogs.com/songsu/p/12096201.html