导航:首页 > 文档加密 > servlet教程pdf

servlet教程pdf

发布时间:2024-03-29 12:29:20

⑴ 如何在jsp中直接打开本地硬盘上的pdf等文件

jsp中要利用java来实现打开,可以通过浏览器打开:
以下程序实现了读取某个路径下的pdf文件,并用浏览器打开:
package test;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class PDFServlet extends HttpServlet {
private static final long serialVersionUID = -3065671125866266804L;
public PDFServlet() {
super();
}

public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/pdf");

FileInputStream in = new FileInputStream(new File("d:/1.pdf"));
OutputStream out = response.getOutputStream();
byte[] b = new byte[512];

while ((in.read(b)) != -1) {
out.write(b);
}

out.flush();
in.close();
out.close();
}

public void init() throws ServletException {
}
}

⑵ jsp中怎么利用java需要将在oracle数据库中存在的pdf,doc等文件下载下来。最好有代码

首先你要明确一个概念,数据库中是不可能存这些文件的,存的最多是这些文件对应的地址,是String类型的数据。
在这基础上来看这些代码。注意标注的1234:
//获取网站部署路径(通过ServletContext对象),用于确定下载文件位置,从而实现下载
String path = servletContext.getRealPath("/");

//1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
response.setContentType("multipart/form-data");
//2.设置文件头:最后一个参数是设置下载文件名(假如我们叫a.pdf)
response.setHeader("Content-Disposition", "attachment;fileName="+"a.pdf");
ServletOutputStream out;
//通过文件路径获得File对象(假如此路径中有一个download.pdf文件)
File file = new File(path + "download/" + "download.pdf");

try {
FileInputStream inputStream = new FileInputStream(file);

//3.通过response获取ServletOutputStream对象(out)
out = response.getOutputStream();

int b = 0;
byte[] buffer = new byte[512];
while (b != -1){
b = inputStream.read(buffer);
//4.写到输出流(out)中
out.write(buffer,0,b);
}
inputStream.close();
out.close();
out.flush();

} catch (IOException e) {
e.printStackTrace();
}

⑶ 如何把springmvc model 生成pdf文件

本文先叙述,如何操作PDF模板生成PDF文件,再说明在SpringMVC中如何根据PDF模板生成PDF文件。
使用PDF模板生成PDF文件需要以下几个步骤:
下面按步骤说明:
1. 使用Microsoft Office Word画好模板
此步骤就不详述了,就是一个普通的Word文件(template.docx)。给个示例截图:

2. 使用Adobe Acrobat X Pro将Word文件转换为带表单字段的PDF模板文件
1) 打开Adobe Acrobat X Pro
2) 选择“创建PDF表单”
3) 选择源:(PDF、Word、Excel或其它文件类型),下一步
4) 定位Word文件路径,下一步
5) Adobe Acrobat X Pro会自动猜测表单字段位置,如图

6) 一般生成的表单字段都不符合我们的要求,选中删除即可。
7) 点击右键选择文本框,拖动到适当的位置,设置好域名称,字号,字体等。

8) 保存模板文件。(template.pdf)
3. 使用itext操作PDF模板,填充数据,生成PDF文件
1) 需要jar包:itext.jar、itextAsian.jar
2) 核心代码:
package personal.hutao.test;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;

public class TestPdf {

@Test
public void test() throws IOException, DocumentException {
String fileName = "D:/template.pdf"; // pdf模板
PdfReader reader = new PdfReader(fileName);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PdfStamper ps = new PdfStamper(reader, bos);
AcroFields fields = ps.getAcroFields();
fillData(fields, data());
ps.setFormFlattening(true);
ps.close();
OutputStream fos = new FileOutputStream("D:/contract.pdf");
fos.write(bos.toByteArray());
}

public void fillData(AcroFields fields, Map<String, String> data) throws IOException, DocumentException {
for (String key : data.keySet()) {
String value = data.get(key);
fields.setField(key, value);
}
}

public Map<String, String> data() {
Map<String, String> data = new HashMap<String, String>();
data.put("borrower", "胡桃同学");
return data;
}
}

3) 打开contract.pdf,如图
至此,就实现了根据PDF模板生成PDF文件。
SpringMVC的视图中已提供了对PDF模板文件的支持:org.springframework.web.servlet.view.document.AbstractPdfStamperView。那么只需要配置好此视图就可以了。具体分为以下步骤:
1) 实现抽象类 AbstractPdfStamperView
package personal.hutao.view;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.view.document.AbstractPdfStamperView;

import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.PdfStamper;

public class PdfStamperView extends AbstractPdfStamperView {

public static final String DATA = "data";
public static final String FILENAME = "mergePdfFileName";

@SuppressWarnings("unchecked")
@Override
protected void mergePdfDocument(Map<String, Object> model,
PdfStamper stamper, HttpServletRequest request,
HttpServletResponse response) throws Exception {
response.setHeader("Content-Disposition", "attachment;filename=" + new String(model.get(FILENAME).toString().getBytes(), "ISO8859-1"));
AcroFields fields = stamper.getAcroFields();
fillData(fields, (Map<String, String>) model.get(DATA));
stamper.setFormFlattening(true);
}

private void fillData(AcroFields fields, Map<String, String> data)
throws IOException, DocumentException {
for (String key : data.keySet()) {
String value = data.get(key);
fields.setField(key, value);
}
}

}

2) 在SpringMVC的配置文件中配置视图
<!-- 按照BeanName解析视图 -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
<property name="order" value="1" />
</bean>

<!-- 定义Pdf模版视图 -->
<bean id="contract" class="personal.hutao.view.PdfStamperView">
<property name="url" value="/WEB-INF/template/template.pdf" />
</bean>

3) Controller中的业务逻辑处理
package personal.hutao.controller;

import static personal.hutao.view.PdfStamperView.DATA;
import static personal.hutao.view.PdfStamperView.FILENAME;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.coamctech.sample.commons.controller.BaseController;

@RequestMapping("/contract")
@Controller
public class TestController {

@RequestMapping("/export/pdf")
public String (Model model) {
model.addAttribute(DATA, data());
model.addAttribute(FILENAME, "XXX贷款合同");
return "contract";
}

private Map<String, String> data() {
Map<String, String> data = new HashMap<String, String>();
data.put("borrower", "胡桃同学");
return data;
}
}

⑷ java中怎么利用struts2上传多个pdf文件

通过3种方式模拟多个文件上传
第一种方式
package com.ljq.action;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class UploadAction extends ActionSupport{
private File[] image; //上传的文件
private String[] imageFileName; //文件名称
private String[] imageContentType; //文件类型
public String execute() throws Exception {
ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
System.out.println(realpath);
if (image != null) {
File savedir=new File(realpath);
if(!savedir.getParentFile().exists())
savedir.getParentFile().mkdirs();
for(int i=0;i<image.length;i++){
File savefile = new File(savedir, imageFileName[i]);
FileUtils.File(image[i], savefile);
}
ActionContext.getContext().put("message", "文件上传成功");
}
return "success";
}
public File[] getImage() {
return image;
}
public void setImage(File[] image) {
this.image = image;
}
public String[] getImageContentType() {
return imageContentType;
}
public void setImageContentType(String[] imageContentType) {
this.imageContentType = imageContentType;
}
public String[] getImageFileName() {
return imageFileName;
}
public void setImageFileName(String[] imageFileName) {
this.imageFileName = imageFileName;
}
}
第二种方式
package com.ljq.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 使用数组上传多个文件
*
* @author ljq
*
*/
@SuppressWarnings("serial")
public class UploadAction2 extends ActionSupport{
private File[] image; //上传的文件
private String[] imageFileName; //文件名称
private String[] imageContentType; //文件类型
private String savePath;
@Override
public String execute() throws Exception {
ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
//取得需要上传的文件数组
File[] files = getImage();
if (files !=null && files.length > 0) {
for (int i = 0; i < files.length; i++) {
//建立上传文件的输出流, getImageFileName()[i]
System.out.println(getSavePath() + "\\" + getImageFileName()[i]);
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getImageFileName()[i]);
//建立上传文件的输入流
FileInputStream fis = new FileInputStream(files[i]);
byte[] buffer = new byte[1024];
int len = 0;
while ((len=fis.read(buffer))>0) {
fos.write(buffer, 0, len);
}
fos.close();
fis.close();
}
}
return SUCCESS;
}
public File[] getImage() {
return image;
}
public void setImage(File[] image) {
this.image = image;
}
public String[] getImageFileName() {
return imageFileName;
}
public void setImageFileName(String[] imageFileName) {
this.imageFileName = imageFileName;
}
public String[] getImageContentType() {
return imageContentType;
}
public void setImageContentType(String[] imageContentType) {
this.imageContentType = imageContentType;
}
/**
* 返回上传文件保存的位置
*
* @return
* @throws Exception
*/
public String getSavePath() throws Exception {
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}
第三种方式
package com.ljq.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 使用List上传多个文件
*
* @author ljq
*
*/
@SuppressWarnings("serial")
public class UploadAction3 extends ActionSupport {
private List<File> image; // 上传的文件
private List<String> imageFileName; // 文件名称
private List<String> imageContentType; // 文件类型
private String savePath;
@Override
public String execute() throws Exception {
ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
// 取得需要上传的文件数组
List<File> files = getImage();
if (files != null && files.size() > 0) {
for (int i = 0; i < files.size(); i++) {
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getImageFileName().get(i));
FileInputStream fis = new FileInputStream(files.get(i));
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fis.close();
fos.close();
}
}
return SUCCESS;
}
public List<File> getImage() {
return image;
}
public void setImage(List<File> image) {
this.image = image;
}
public List<String> getImageFileName() {
return imageFileName;
}
public void setImageFileName(List<String> imageFileName) {
this.imageFileName = imageFileName;
}
public List<String> getImageContentType() {
return imageContentType;
}
public void setImageContentType(List<String> imageContentType) {
this.imageContentType = imageContentType;
}
/**
* 返回上传文件保存的位置
*
* @return
* @throws Exception
*/
public String getSavePath() throws Exception {
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}
struts.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 该属性指定需要Struts2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。
如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 -->
<constant name="struts.action.extension" value="do" />
<!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->
<constant name="struts.serve.static.browserCache" value="false" />
<!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 -->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->
<constant name="struts.devMode" value="true" />
<!-- 默认的视图主题 -->
<constant name="struts.ui.theme" value="simple" />
<!--<constant name="struts.objectFactory" value="spring" />-->
<!--解决乱码 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<constant name="struts.multipart.maxSize" value="10701096"/>
<package name="upload" namespace="/upload" extends="struts-default">
<action name="*_upload" class="com.ljq.action.UploadAction" method="{1}">
<result name="success">/WEB-INF/page/message.jsp</result>
</action>
</package>
<package name="upload1" namespace="/upload1" extends="struts-default">
<action name="upload1" class="com.ljq.action.UploadAction2" method="execute">
<!-- 要创建/image文件夹,否则会报找不到文件 -->
<param name="savePath">/image</param>
<result name="success">/WEB-INF/page/message.jsp</result>
</action>
</package>
<package name="upload2" namespace="/upload2" extends="struts-default">
<action name="upload2" class="com.ljq.action.UploadAction3" method="execute">
<!-- 要创建/image文件夹,否则会报找不到文件 -->
<param name="savePath">/image</param>
<result name="success">/WEB-INF/page/message.jsp</result>
</action>
</package>
</struts>
上传表单页面upload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>文件上传</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<!-- ${pageContext.request.contextPath}/upload/execute_upload.do -->
<!-- ${pageContext.request.contextPath}/upload1/upload1.do -->
<!-- ${pageContext.request.contextPath}/upload2/upload2.do -->
<!-- -->
<form action="${pageContext.request.contextPath}/upload2/upload2.do" enctype="multipart/form-data" method="post">
文件1:<input type="file" name="image"><br/>
文件2:<input type="file" name="image"><br/>
文件3:<input type="file" name="image"><br/>
<input type="submit" value="上传" />
</form>
</body>
</html>
显示页面message.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'message.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
上传成功
<br/>
<s:debug></s:debug>
</body>
</html>

⑸ 《JSP&Servlet学习笔记》pdf下载在线阅读,求百度网盘云资源

《JSP & Servlet学习笔记》(【台湾】林信良)电子书网盘下载免费在线阅读

资源链接:

链接:https://pan..com/s/1oil-SMK44sHQVnO1lg2PXA

提取码:qvj6

书名:JSP & Servlet学习笔记

作者:【台湾】林信良

豆瓣评分:8.6

出版社:清华大学出版社

出版年份:2012-5

页数:463

内容简介:本书是作者多年来教学实践经验的总结,汇集了教学过程中学生在学习JSP & Servlet时遇到的概念、操作、应用或认证考试等问题及解决方案。

本书针对Servlet 3.0的新功能全面改版,无论是章节架构与范例程序代码,都做了全面更新。书中详细介绍了Servlet/ JSP与Web容器之间的关系,必要时从Tomcat源代码分析,了解Servlet/ JSP如何与容器互动。本书还涵盖了文本处理、图片验证、自动登录、验证过滤器、压缩处理、JSTL应用与操作等各种实用范例。

本书在讲解的过程中,以“微博”项目贯穿全书,随着每一章的讲述都在适当的时候将JSP & Servlet技术应用于“微博”程序之中,以便读者能了解完整的应用程序构建方法。

作者简介:林信良(网名:良葛格)

学历:台湾大学电机工程学系

经历:台湾升阳教育训练技术顾问、专业讲师,Oracle授权训练中心讲师

着作:《Java JDK 5.0学习笔记》、《Java SE 6技术手册》、《Spring技术手册》等

译作:《Ajax实战手册》、《jQuery实战手册(第2版)》

个人网站:http://openhome.cc

⑹ servlet 、jsp有像java基础一样的API文档吗

servlet和jsp也有像java一样的api文档。一般做成chm格式供开发人员参考。

比如在网络中搜索servlet帮助文档,点击网络一下:

点击进去按照要求下载即可。

用同样的方法可以下载到jsp的办帮助文档。

⑺ jsp页面如何导成pdf格式

先在服务器上生成PDF文件,然后用户通过点击指向PDF文件的超链接选择下载或打开。这是一个思路,或者说是思路之一。本文实现了这个思路,又给出另外一个思路并通过两种途径实现之。

1)直接在服务器上生成PDF文件。

<%@ page import ="com.lowagie.text.*
,com.lowagie.text.pdf.*, java.io.*"%>
<%
String filename =
"PDF"+(new Random()).nextInt()+".pdf" ;
Document document =
new Document(PageSize.A4);
ServletOutputStream out1
= response.getOutputStream();
try{
PdfWriter writer =
PdfWriter.getInstance(document,
new FileOutputStream(filename) );
document.open();
document.add(new Paragraph("Hello World"));
document.close();
}
catch(Exception e){}
%>

上面的程序在服务器上生成了一个静态的PDF文件。显然,每次运行所得的PDF文件的名称应该是独一无二不能有重的。本程序通过随机函数来命名生成的PDF文件。本程序的缺点就是,每次运行都会在服务器上产生一个PDF文件,如果不及时删除,数量会越来越大,这显然是站点维护者所不愿意看到的。

2)将PDF文件通过流的形式输送到客户端的缓存。这样做的好处是不会在服务器上留下任何“遗迹”。

i)直接通过JSP页面生成

<%@
page import="java.io.*,
java.awt.Color,com.lowagie.text.*,
com.lowagie.text.pdf.*"%>
<%
response.setContentType
( "application/pdf" );
Document document = new Document();
ByteArrayOutputStream buffer
= new ByteArrayOutputStream();
PdfWriter writer=
PdfWriter.getInstance( document, buffer );
document.open();
document.add(new Paragraph("Hello World"));
document.close();
DataOutput output =
new DataOutputStream
( response.getOutputStream() );
byte[] bytes = buffer.toByteArray();
response.setContentLength(bytes.length);
for( int i = 0;
i < bytes.length;
i++ )
{
output.writeByte( bytes[i] );
}
%>

ii)通过Servlet生成

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
public void doGet
(HttpServletRequest request,
HttpServletResponse response)
throws IOException,ServletException
{
Document document =
new Document(PageSize.A4, 36,36,36,36);
ByteArrayOutputStream ba
= new ByteArrayOutputStream();
try
{
PdfWriter writer =
PdfWriter.getInstance(document, ba);
document.open();
document.add(new
Paragraph("Hello World"));
}
catch(DocumentException de)
{
de.printStackTrace();
System.err.println
("A Document error:" +de.getMessage());
}
document.close();
response.setContentType
("application/pdf");
response.setContentLength(ba.size());
ServletOutputStream out
= response.getOutputStream();
ba.writeTo(out);
out.flush();
}

阅读全文

与servlet教程pdf相关的资料

热点内容
6x15x25简便算法 浏览:281
书邻是什么电影 浏览:352
泰国电影蛇女国语版 浏览:781
舒茨pdf 浏览:73
冈本app旧版本在哪里下载 浏览:259
cpp命令 浏览:330
阴阳师安卓版和双端版什么区别 浏览:903
不用下载的网页电影 浏览:470
哪里有大香蕉APP下载 浏览:388
逆光源压缩包 浏览:659
电脑看s片的网站 浏览:542
饥荒启动服务器为什么这么慢 浏览:499
pdf转jpg破解版 浏览:344
用什么软件把图片加密 浏览:692
云比特开发源码 浏览:850
哪里有成人免费电影网址 浏览:841
怎么把mac桌面文件夹复制到硬盘 浏览:367
腾讯云境内外服务器内网通讯 浏览:180
老款的摩宝app在哪里可以下载 浏览:229
美赞臣溯源码被多次查验正常么 浏览:879