创建目录
D:\img
配置tomacat的server.xml(推荐在eclipse的Servers中的tomacat配置文件中修改)
Host标签最后添加下面的标签
<Context docBase="D:\img" path="/img"/>
编写servlet
/**
* <p>Title: doAddfruit</p>
* <b>Description:添加商品</b><br>
* @param request
* @param response
* @throws ServletException
* @throws IOException
* <b>Author:</b> Lsc
* <br><b>Date:</b> 2018年6月17日 下午9:04:08
* <br><b>Version:</b> 1.0
* @Note
*
*/
private void doAddfruit(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding(
"utf-8");
List<String> friutAttrbuteList =
new ArrayList<String>();
String realPath =
null;
String name =
null;
try {
DiskFileItemFactory factory =
new DiskFileItemFactory();
ServletFileUpload servletFileUpload =
new ServletFileUpload(factory);
if (servletFileUpload.isMultipartContent(request)) {
List<FileItem> parseRequest = servletFileUpload.parseRequest(request);
for (FileItem fileItem : parseRequest) {
if (fileItem.isFormField()) {
String friutAttrbute = fileItem.getString(
"UTF-8");
friutAttrbuteList.add(friutAttrbute);
}
else {
name = fileItem.getName();
name = UUID.randomUUID().toString().replaceAll(
"_",
"")+
"$"+name;
realPath=
"/img";
File file =
new File(realPath);
if (!file.exists()) {
file.mkdir();
}
fileItem.write(
new File(file,name));
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}
int fid = Integer.parseInt(friutAttrbuteList.get(
0));
String fname = friutAttrbuteList.get(
1);
String spec = friutAttrbuteList.get(
2);
double up = Double.parseDouble(friutAttrbuteList.get(
3));
String t1 = friutAttrbuteList.get(
4);
String t2 = friutAttrbuteList.get(
5);
int inum =
1;
int stock = Integer.parseInt(friutAttrbuteList.get(
6));
String path = realPath+
"\\"+name;
Fruit fruit =
new Fruit(fid, fname, spec, up, t1, t2, inum,stock,path);
boolean boo = fServiceI.add(fruit);
if (boo)
doAllfruit(request, response);
else
request.getRequestDispatcher(
"BSindex5.jsp").forward(request, response);
}
图片显示
<img src="+fruit.getPath()+">
tips: 创建虚拟目录的另一种方法: 在Tomcat根目录下的/conf/Catalina/localhost/ 路径下新建一个filename.xml,并在该xml中编写语句 <Context docBase="F:\Java\JavaWebWorkspace\news"/> 即可创建虚拟站点,虚拟站点名为filename。注意docbase指向你自己的应用程序目录,各参数参见方法1中的标签的参数(注意此文件名将作为Context中的path属性值,不管文件里的path属性值如何设置也是无效的 )。 如果需要分层虚拟站点,可以将文件名改成a#b#c,访问分层虚拟站点时用localhost:8080/a/b/c Context标签详解: <Context path="/yang" docBase="f:\mysite1\xxx.war" debug="0" reloadable="true" crossContext="true" />
说明:
path:指定访问该 Web 应用的 URL 入口。docBase:指定 Web 应用的文件路径,可以给定绝对路径,也可以给定相对于<Host>的appBase 属性的相对路径,如果 Web 应用采用开放目录结构,则指定 Web 应用的根目录,如果 Web 应用是个 war 文件,则指定 war 文件的路径。(指定项目所在地址)reloadable:如果这个属性设为 true,tomcat 服务器在运行状态下会监视在WEB-INF/classes 和 WEB-INF/lib 目录下 class 文件的改动,如果监测到有 class 文件被更新的,服务器会自动重新加载 Web 应用。crossContext:如果想在应用内调用 ServletContext.getContext()来返回在该虚拟主机上运行的其他 web application 的 request dispatcher,设为 true。在安全性很重要的环境中设为 false,使得 getContext()总是返回 null。缺省值为 false。Debug:与这个 Engine 关联的 Logger 记录的调试信息的详细程度。数字越大,输出越详细。如果没有指定,缺省为 0。这个方法一般在设置后都需要重启服务器才有效(有时候即使设置了reloadable=“true” 也需要重新启动)