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

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

記得要找到Host標籤。在這個標籤裏面添加web

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

最終效果是這樣的:apache

<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>

 

在後臺用的時候就能夠這樣寫tomcat

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://Fileapp

idea裏面在配置一個東西webapp

 

在上面的HTTP port 右側點上勾jsp

就能夠用在jsp使用了ide

 

<img id="image" src="/File${product.productImage}" style="width: 100px" >
相關文章
相關標籤/搜索