这里我直接写maven依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
<type>pom.lastUpdated</type>
</dependency>
<form action="Aservlet" method="post" enctype="multipart/form-data">
<input type="text" name="tname" />
<input type="file" name="tfile" />
<button type="submit">SSSS</button>
</form>
这里注意enctype的值
PrintWriter out=response.getWriter();
response.setContentType("text/html");
try {
DiskFileItemFactory factory=new DiskFileItemFactory();
ServletFileUpload upload=new ServletFileUpload(factory);
upload.setHeaderEncoding("utf-8");
if(!ServletFileUpload.isMultipartContent(request)) {
return;
}
List<FileItem> list=upload.parseRequest(request);
for (FileItem fileItem : list) {
if(fileItem.isFormField()) {
//处理表单内非文件内容
String name=fileItem.getFieldName();
String value=fileItem.getString("UTF-8");
}else {
String filename=fileItem.getName();
if(filename.endsWith(".png")) {
//这里只接收以png结尾的文件
filename=filename.substring(filename.lastIndexOf("\\")+1);
//得到文件输入流
InputStream inputStream=fileItem.getInputStream();
//创建一个新数组存放内容
byte data[]=new byte[inputStream.available()];
//写入数组
inputStream.read(data);
/*byte buffer[]=new byte[1024];
int len=0;
while((len=inputStream.read(buffer))>0) {
outputStream.write(buffer, 0, len);
}*/
inputStream.close();
//用base64编码为字符串,接下来就能存入数据库(未写)
String str=Base64.getEncoder().encodeToString(data);
request.getSession().setAttribute("img", str);
fileItem.delete();
}
else {
continue;
}
}
}
}catch (Exception e) {
// TODO: handle exception
}
request.getRequestDispatcher("img.jsp").forward(request, response);
这里是doget()里的内容
//禁止缓存
response.setDateHeader("Expires", -1);
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
// 通知jsp以图片方式打开发送过去的数据
response.setHeader("Content-Type", "image/jpeg");
String str=(String) request.getSession().getAttribute("img");
OutputStream outputStream=response.getOutputStream();
byte data[]=Base64.getDecoder().decode(str);
outputStream.write(data);
outputStream.close();
base64解码,用response的输出流输出
<label>图片:</label>
<img width="200" height="200" src="${pageContext.request.contextPath}/Imgg"/>
<a href="${pageContext.request.contextPath}/DownS">hahah</a>
可以设置图片的高和宽
通过上面的超链接请求downs这个servlet下载图片
doget()内容
response.setDateHeader("Expires", -1);
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
response.setHeader("content-disposition", "attachment;");
String str=(String) request.getSession().getAttribute("img");
OutputStream outputStream=response.getOutputStream();
byte data[]=Base64.getDecoder().decode(str);
outputStream.write(data);
outputStream.close();
遇到的错误或异常:
使用EL表达式标红
缺少commons-logging