java上傳pdf
你好,很高興回答你的問題。
要實現這個功能你需要查看一下騰訊雲有沒有開放對應的api供調用。
如果開放了,按照對應的api規則進行編碼調用就可以。
如果有幫助到你,請點擊採納。
2. java生成pdf,圖片怎麼導不進去
不知道你用的什麼方法來導的,但是如果通過PDF類庫jar包來實現的話,應該是沒問題的,參考如下java代碼中關於如何插入圖片到PDF的方法:
import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
public class AddImage {
public static void main(String[] args) {
//創建文檔
PdfDocument pdf = new PdfDocument();
//添加一頁
PdfPageBase page = pdf.getPages().add();
//載入圖片,並獲取圖片高寬
PdfImage image = PdfImage.fromFile("fj.png");
int width = image.getWidth()/2;
int height = image.getHeight()/2;
//繪制圖片到PDF
page.getCanvas().drawImage(image,50,50,width, height);
//保存文檔
pdf.saveToFile("result.pdf");
pdf.dispose();
}}
3. 如何運用Java組件itext生成pdf
Controller層(param為數據)
byte[]bytes=PdfUtils.createPdf(param);
ByteArrayInputStreaminStream=newByteArrayInputStream(bytes);
//設置輸出的格式
response.setContentType("bin");
response.setHeader("content-disposition","attachment;filename="+URLEncoder.encode(itemName+"(評審意見).pdf","UTF-8"));
//循環取出流中的數據
byte[]b=newbyte[2048];
intlen;
while((len=inStream.read(b))>0)
response.getOutputStream().write(b,0,len);
inStream.close();
Service層(param為數據)
public static byte[] createPdf(Map<String,Object> param) {
byte[] result= null;
ByteArrayOutputStream baos = null;
//支流程評審信息匯總
List<CmplncBranchRvwInfoDTO> branchRvwInfos = (List<CmplncBranchRvwInfoDTO>) param.get("branchRvwInfos");
//主流程評審信息匯總
List<CmplncMainRvwInfoDTO> mainRvwInfos = (List<CmplncMainRvwInfoDTO>) param.get("mainRvwInfos");
//主評審信息
CmplncRvwFormDTO rvwFormDTO = (CmplncRvwFormDTO) param.get("rvwFormDTO");
//附件列表
List<FileInfoDTO> fileList = (List<FileInfoDTO>) param.get("fileList");
//專業公司
String legalEntityDeptDesc = (String) param.get("legalEntityDeptDesc");
String legalEntitySonDeptDesc = (String) param.get("legalEntitySonDeptDesc");
//設置頁邊距
Document doc = new Document(PageSize.A4, 20, 20, 60, 20);
try {
baos = new ByteArrayOutputStream();//構建位元組輸出流
PdfWriter writer = PdfWriter.getInstance(doc,baos);
//頁眉頁腳字體
BaseFont bf = null;
BaseFont bFont = null;
try {
bFont = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
} catch (Exception e) {
e.printStackTrace();
}
Font footerFont = new Font(bFont, 10, Font.NORMAL);
Font title = new Font(bf,15,Font.BOLD);
Font content = new Font(bf,9,Font.NORMAL);
Font chinese = new Font(bf, 10, Font.BOLD);
/**
* HeaderFooter的第2個參數為非false時代表列印頁碼
* 頁眉頁腳中也可以加入圖片,並非只能是文字
*/
HeaderFooter header=new HeaderFooter(new Phrase("法律合規評審系統",title),false);
//設置是否有邊框等
header.setBorder(Rectangle.NO_BORDER);
header.setAlignment(1);
doc.setHeader(header);
HeaderFooter footer=new HeaderFooter(new Phrase("-",footerFont),new Phrase("-",footerFont));
/**
* 0左 1中 2右
*/
footer.setAlignment(1);
footer.setBorder(Rectangle.NO_BORDER);
doc.setFooter(footer);
doc.open();
//doc.add(new Paragraph("評審意見:",chinese));
//7列
PdfPTable table = new PdfPTable(7);
PdfPCell cell;
table.addCell(new Paragraph("評審項目編號",chinese));
table.addCell(new Paragraph(rvwFormDTO.getRvwItemCode(),content));
cell = new PdfPCell(new Paragraph("評審項目類型",chinese));
cell.setColspan(2);
table.addCell(cell);
String rvwItemParentType = (String) param.get("rvwItemParentType");
String rvwItemType = (String) param.get("rvwItemType");
String rvwItemTwoType = (String) param.get("rvwItemTwoType");
cell = new PdfPCell(new Paragraph(rvwItemParentType+"/"+rvwItemType+"/"+rvwItemTwoType,content));
cell.setColspan(3);
table.addCell(cell);
table.addCell(new Paragraph("申請人姓名",chinese));
table.addCell(new Paragraph(rvwFormDTO.getApplicantName(),content));
cell = new PdfPCell(new Paragraph("申請人所在部門",chinese));
cell.setColspan(2);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(rvwFormDTO.getAplcntDeptName(),content));
cell.setColspan(3);
table.addCell(cell);
table.addCell(new Paragraph("錄入人姓名",chinese));
table.addCell(new Paragraph(rvwFormDTO.getRecordName(),content));
cell = new PdfPCell(new Paragraph("項目涉及金額",chinese));
cell.setColspan(2);
table.addCell(cell);
table.addCell(new Paragraph(String.valueOf(rvwFormDTO.getInvolveAmount()==null?0:rvwFormDTO.getInvolveAmount()),content));
table.addCell(new Paragraph("幣種",chinese));
String involveAmountType = (String) param.get("involveAmountType");
table.addCell(new Paragraph(involveAmountType,content));
table.addCell(new Paragraph("專業公司",chinese));
cell = new PdfPCell(new Paragraph(legalEntityDeptDesc+"->"+legalEntitySonDeptDesc,content));
cell.setColspan(6);
table.addCell(cell);
table.addCell(new Paragraph("項目名稱",chinese));
cell = new PdfPCell(new Paragraph(rvwFormDTO.getItemName(),content));
cell.setColspan(6);
table.addCell(cell);
table.addCell(new Paragraph("項目概述",chinese));
cell = new PdfPCell(new Paragraph(rvwFormDTO.getItemOverview(),content));
cell.setColspan(6);
table.addCell(cell);
table.addCell(new Paragraph("評審需求",chinese));
cell = new PdfPCell(new Paragraph(rvwFormDTO.getRvwDemand(),content));
cell.setColspan(6);
table.addCell(cell);
table.addCell(new Paragraph("申請人自我評估",chinese));
cell = new PdfPCell(new Paragraph(rvwFormDTO.getApplicantSelfAssmnt(),content));
cell.setColspan(6);
table.addCell(cell);
/* table.addCell(new Paragraph("同步抄送",chinese));
cell = new PdfPCell(new Paragraph(rvwFormDTO.getSyncsenderNames(),content));
cell.setColspan(6);
table.addCell(cell);*/
int infoNum = 0;
if(fileList.size() > 0){
//附件信息
cell = new PdfPCell(new Paragraph("附件信息",chinese));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
cell.setRowspan(fileList.size()+1);
table.addCell(cell);
//序號
cell = new PdfPCell(new Paragraph("序號",chinese));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
//附件名稱
cell = new PdfPCell(new Paragraph("附件名稱",chinese));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
//上傳時間
cell = new PdfPCell(new Paragraph("上傳時間",chinese));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
//上傳人
cell = new PdfPCell(new Paragraph("上傳人",chinese));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
//附件類型
cell = new PdfPCell(new Paragraph("附件類型",chinese));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
//法律合規評審
cell = new PdfPCell(new Paragraph("法律合規評審",chinese));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
for (FileInfoDTO file : fileList) {
infoNum++;
//序號
cell = new PdfPCell(new Paragraph(infoNum+"",content));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
//附件名稱
cell = new PdfPCell(new Paragraph(file.getFileName(),content));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
//上傳時間
cell = new PdfPCell(new Paragraph(file.getUploadTimeFormat(),content));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
//上傳人
cell = new PdfPCell(new Paragraph(file.getUploadName(),content));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
//附件類型
String fileType;
if("1".equals(file.getFileType())){
fileType = "合同文件";
}else if("99".equals(file.getFileType())){
fileType = "支持文檔";
}else{
fileType = "";
}
cell = new PdfPCell(new Paragraph(fileType,content));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
//法律合規評審
String typeRvwStatus;
if("1".equals(file.getTypeRvwStatus())){
typeRvwStatus = "經審查附件無重大法律合規問題";
}else if("2".equals(file.getTypeRvwStatus())){
typeRvwStatus = "退回修改";
}else{
typeRvwStatus = "";
}
cell = new PdfPCell(new Paragraph(typeRvwStatus,content));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
}
}else{
//附件信息
cell = new PdfPCell(new Paragraph("附件信息",chinese));
table.addCell(cell);
cell = new PdfPCell(new Paragraph("沒有附件",content));
cell.setColspan(6);
table.addCell(cell);
}
cell = new PdfPCell(new Paragraph("評審意見",chinese));
cell.setRowspan(mainRvwInfos.size()+branchRvwInfos.size());
//居中
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
if(mainRvwInfos.size()>0){
cell = new PdfPCell(new Paragraph("主評審意見",chinese));
cell.setRowspan(mainRvwInfos.size());
//居中
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
infoNum = 0;
for (CmplncMainRvwInfoDTO dto : mainRvwInfos) {
infoNum++;
//序號
cell = new PdfPCell(new Paragraph(infoNum+"",content));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
//評審意見
PdfPTable branchRvwInfosTable = new PdfPTable(1);
cell = new PdfPCell(new Paragraph(delHTMLTag(dto.getRvwOpinion()),content));
cell.disableBorderSide(2);
branchRvwInfosTable.addCell(cell);
//評審人和評審時間
cell = new PdfPCell(new Paragraph(dto.getRvwUmName()+"/"+getStringDate(dto.getRvwTime()),content));
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell.disableBorderSide(1);
cell.setPaddingTop(5);
branchRvwInfosTable.addCell(cell);
PdfPCell branchRvwInfosCell = new PdfPCell(branchRvwInfosTable);
branchRvwInfosCell.setColspan(4);
table.addCell(branchRvwInfosCell);
}
doc.add(table);
}
if(branchRvwInfos.size()>0){
cell = new PdfPCell(new Paragraph("支評審意見",chinese));
cell.setRowspan(branchRvwInfos.size());
//居中
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
infoNum = 0;
for (CmplncBranchRvwInfoDTO dto : branchRvwInfos) {
infoNum++;
//序號
cell = new PdfPCell(new Paragraph(infoNum+"",content));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
//評審意見
PdfPTable branchRvwInfosTable = new PdfPTable(1);
cell = new PdfPCell(new Paragraph(delHTMLTag(dto.getRvwOpinion()),content));
cell.disableBorderSide(2);
branchRvwInfosTable.addCell(cell);
//評審人和評審時間
cell = new PdfPCell(new Paragraph(dto.getRvwUmName()+"/"+getStringDate(dto.getRvwTime()),content));
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell.disableBorderSide(1);//隱藏上邊框
cell.setPaddingTop(5);
branchRvwInfosTable.addCell(cell);
PdfPCell branchRvwInfosCell = new PdfPCell(branchRvwInfosTable);
branchRvwInfosCell.setColspan(4);
table.addCell(branchRvwInfosCell);
}
doc.add(table);
}
if(doc != null){
doc.close();
}
result =baos.toByteArray();
} catch (DocumentException e) {
e.printStackTrace();
}finally{
if(baos != null){
try {
baos.close();
} catch (IOException e) {
log.error("PDF異常", e);
}
}
}
return result;
}
工具
/**
* 去掉HTML標簽
* @param htmlStr
* @return
*/
public static String delHTMLTag(String htmlStr){
if (htmlStr!=null){
String regEx_script="<script[^>]*?>[\s\S]*?<\/script>"; //定義script的正則表達式
String regEx_style="<style[^>]*?>[\s\S]*?<\/style>"; //定義style的正則表達式
String regEx_html="<[^>]+>"; //定義HTML標簽的正則表達式
Pattern p_script=Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);
Matcher m_script=p_script.matcher(htmlStr);
htmlStr=m_script.replaceAll(""); //過濾script標簽
Pattern p_style=Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
Matcher m_style=p_style.matcher(htmlStr);
htmlStr=m_style.replaceAll(""); //過濾style標簽
Pattern p_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
Matcher m_html=p_html.matcher(htmlStr);
htmlStr=m_html.replaceAll(""); //過濾html標簽
Pattern p_enter = Pattern.compile("\s*| | | ");
Matcher m_enter = p_enter.matcher(htmlStr);
htmlStr = m_enter.replaceAll("");
}
return htmlStr.trim().replaceAll(" ", ""); //返迴文本字元串
}
/**
* @return返回字元串格式 yyyy-MM-dd HH:mm:ss
*/
public static String getStringDate(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(date);
return dateString;
}
4. 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>
5. java中 怎麼實現word、pdf、jpg等一些文件格式的上傳下載 並且還要支持預覽功能 最好有代碼 留qq也可以
jsp的話需要用smartup等上傳插件 另外servlet3.0自帶上傳功能
6. java中,怎麼把一個pdf格式的圖片(是掃描生成的)文件上傳
不能直接下載 先要下載pdf轉word的軟體!接下來就可以了!
7. Java上傳pdf文件,只壓縮大小,不改變成.zip/.rar文件
Java上傳pdf文件,只壓縮大小,不改變成.zip/.rar文件
可以,壓縮只是一種演算法,什麼語言都可以,比如某種格式的文件中1001010(二進制)代表漢子的"中"字,那麼壓縮演算法就是在編碼不沖突的情況下可以改變編碼長度,比如壓縮之後中字變成1010,這樣就節省空間了,這是我隨便舉的例子,具體的對應演算法可以網上查
8. 客戶端點擊網頁鏈接、下載pdf文件、java服務端如何將本地的pdf文件上傳並讓客戶端下載
用ftp把 把文件傳到ftp 再從ftp下載 文件上傳的插件有很多 比如jquery.form 、Uploadify、webuploadify等