關於文件上傳的坑,tomcat一重啟圖片就消失


首先在tomcat的conf的server.xml中配置一個配置.

記得要找到Host標簽。在這個標簽里面添加

 <Context path ="/File" docBase ="D:/File" debug ="0" privileged ="true">
        </Context>

最終效果是這樣的:

 <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->
        <Context path ="/File" docBase ="D:/File" debug ="0" privileged ="true">
        </Context>
        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>

 

在后台用的時候就可以這樣寫

String productName = req.getParameter("productName");
        String productDesc = req.getParameter("productDesc");
        String productPrice = req.getParameter("productPrice");
        Part cover = req.getPart("productImage");
        Long da = new Date().getTime()/1000;
        String filePath = "/"+da+"_"+cover.getSubmittedFileName();
        File file = new File("/File"); //這里是重點
        String destPath = file.getAbsolutePath()+filePath;
        new File(destPath).getParentFile().mkdir();
        cover.write(destPath);
        int categoryId = Integer.parseInt(req.getParameter("categoryId"));
        Product product = new Product(productName,productDesc,new BigDecimal(productPrice),filePath,categoryId);
        ProductService service = new ProductService();
        service.insertProduct(product);
        resp.sendRedirect(req.getContextPath()+"/index");

上面我標記的地方就會去找D://File

idea里面在配置一個東西

 

在上面的HTTP port 右側點上勾

就可以用在jsp使用了

 

  <img id="image" src="/File${product.productImage}" style="width: 100px" >

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM