In my Spring MVC application, there is a need to upload user files. The application runs on tomcat. On the local machine, I specify the path to the folder for saving files so imagesUrl = c: / images / (in the file my.properties) and in the controller I get this property as a path. Everything is working.

Next, I launch the application on the server (Ubuntu 14.04), and in the settings file I change the value to imagesUrl = / images / . Pre-created the folder /images and prescribed it chmod ugo+rw . The catalina.out errors only when trying to get files from the directory. What could be the problem?

EDITED: save:

 public String save(List<Image> images) { if (images.size() == 0) return null; String imagesUrl = generalProperties.getProperty("imagesUrl"); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); for ( int i = 0; i < images.size(); i++ ) { Image image = images.get(i); String location = imagesUrl + image.getUser() + "/" + image.getTitle(); File pathFile = new File(location); if (!pathFile.exists()) { pathFile.mkdir(); } pathFile = new File(location + "/" + image.getName()); try { image.getFile().transferTo(pathFile); } catch (IOException e) { e.printStackTrace(); } session.save(image); if ( i % 20 == 0 ) { session.flush(); session.clear(); } } tx.commit(); session.close(); return "Advert saved successfully"; } 

so I get:

 @RequestMapping(value = "/imageDisplay", method = RequestMethod.GET) public void showImage(HttpServletResponse response, HttpServletRequest request, Model model, @RequestParam("advertId") Integer advertId, @RequestParam("user") String user, @RequestParam("fileName") String fileName) throws ServletException, IOException, SQLException { String imagesUrl = generalProperties.getProperty("imagesUrl"); File file = new File(imagesUrl + user + "/" + advertService.getAdvertById(advertId).getTitle() + "/" + fileName); InputStream inputStream = null; inputStream = new FileInputStream(file); FileCopyUtils.copy(inputStream, response.getOutputStream()); } 
  • Add the bug itself, spectrays - MrFylypenko
  • where to see it? - Dmitry
  • which catalina.out, what kind of Exception? - MrFylypenko
  • Well, the code itself, as you save and read the pictures - MrFylypenko
  • I cleared the logs folder and catalina.out is not re-created (( - Dmitry

1 answer 1

Navayal servlet in a hurry.

 @WebServlet(name = "test", urlPatterns = "/test") public class test extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Получаем разделитель файлов операционной системы // windows - '\' UNIX - '/' String fs = System.getProperty("file.separator"); // В моем случае tomcat установлен в каталоге // /opt/apache-tomcat // Получаем изображение // Путь начинается от каталога /opt/apache-tomcat/bin/{your-app-name}/WEB-INF/classes InputStream is = test.class.getResourceAsStream(fs + "noimage.png"); // Записываем изображение // Путь начинается от каталога /opt/apache-tomcat/bin/ FileOutputStream fos = new FileOutputStream(".." + fs + "images" + fs + "filename.png"); // Записываем файл int read; while ((read = is.read()) != -1) { fos.write(read); } response.getWriter().println("done"); } }