通过jsp页面上传图片至服务器指定目录,上传成功但服务器中的图片不能正常显示,求解? 5
jsp:<formaction=""method="post"enctype="multipart/form-data"name="form1"><inputtype="...
jsp:
<form action="" method="post" enctype="multipart/form-data" name="form1">
<input type="file" name="file"/>
<input type="button" value="上传" onclick="getJobnum2()"/>
</form>
servlet:
//取得二进制流
ServletInputStream sis = req.getInputStream();
//与输入缓冲流接上
BufferedInputStream bis = new BufferedInputStream(sis);
//获取输出流的物理路径
String imgname = req.getParameter("jobnum");
String path = Utils.getSysParam("PIC_TMP") + "/TMP_" + imgname + ".jpg";
//文件输出流
FileOutputStream fos = new FileOutputStream(path);
//缓冲输出流
BufferedOutputStream bos = new BufferedOutputStream(fos);
//从输入流中读取数据
byte[] buffer = new byte[1024];
int len = 0;
while((len = bis.read(buffer)) != -1) {
//输出读入的数据
bos.write(buffer, 0, len);
}
//关闭流
bos.close();
fos.close();
bis.close();
sis.close(); 展开
<form action="" method="post" enctype="multipart/form-data" name="form1">
<input type="file" name="file"/>
<input type="button" value="上传" onclick="getJobnum2()"/>
</form>
servlet:
//取得二进制流
ServletInputStream sis = req.getInputStream();
//与输入缓冲流接上
BufferedInputStream bis = new BufferedInputStream(sis);
//获取输出流的物理路径
String imgname = req.getParameter("jobnum");
String path = Utils.getSysParam("PIC_TMP") + "/TMP_" + imgname + ".jpg";
//文件输出流
FileOutputStream fos = new FileOutputStream(path);
//缓冲输出流
BufferedOutputStream bos = new BufferedOutputStream(fos);
//从输入流中读取数据
byte[] buffer = new byte[1024];
int len = 0;
while((len = bis.read(buffer)) != -1) {
//输出读入的数据
bos.write(buffer, 0, len);
}
//关闭流
bos.close();
fos.close();
bis.close();
sis.close(); 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询