在ASP.NET应用程序中,我使用iTextSharp(主要是PdfStamper)来填充PDF上的一些内容并将其发送给用户.以下代码位于OnClick事件中:
PdfReader r = new PdfReader(
new RandomAccessFileOrArray(Request.MapPath(compatiblePdf)), null
);
ps = new PdfStamper(r, Response.OutputStream);
AcroFields af = ps.AcroFields;
af.SetField("ContactInfo[0]", o.mallName);
af.SetField("ClientName", string.Format("{0} {1}", c.firstName, c.lastName));
af.SetField("ClientEmail", c.emailAddress);
ps.FormFlattening = true;
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=Form.pdf");
ps.Close();
Response.End();
Run Code Online (Sandbox Code Playgroud)
因此,基本上,PdfReader获取文件,PdfStamper将PdfReader作为参数,并将其完成的PDF推送到Response.OutputStream.
问题是,使用IE和Adobe Reader,如果从文件对话框中选择"打开",Adobe Reader会抛出错误提示"无法找到文件".用户可以"保存"文件,甚至可以再次开始下载(在被问到时再次单击"打开")似乎可以正常工作.但是在一台永远不会下载文件的新机器上,Adobe Reader似乎错误地将文件放在临时文件或IE之间.
我现在只能想象一件事:Response.End()或许,应该,或许,Response.Close()或许整个事情应该Response.Flush()在它之前.但我不确定这会不会使问题变得更糟,而且我很难测试(因为,一旦你下载了一次文件,就不会再次抛出错误).
可能解决问题吗?我在标题中有什么问题吗?或者我还应该对Response/PdfStamper对象做些什么呢?
我正在编写一个Java程序,用于在设置的时间间隔内调用PHP脚本.PHP脚本输出大量数据,并且客户端要求Java程序在脚本运行时显示所有PHP脚本的输出.
我正在使用的方法是:
Runtime.getRuntime().exec(new String[]{"php", "file.php"});
Run Code Online (Sandbox Code Playgroud)
然后使用a InputStreamReader来获取输出,主要使用此处的示例.我遇到的问题是,当PHP脚本退出时,流阅读器只输出数据(考虑到输出是如何循环的,这是有意义的).
在脚本运行时,如何实时打印脚本的输出?
我正在尝试使用outputstream从URL保存文件.该URL由https保护.所以当我尝试获取以下文件时出现了一些错误
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.ssl.Alerts.getSSLException(Unknown Source)
at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source)
at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
at java.net.URL.openStream(Unknown Source)
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)
at java.security.cert.CertPathBuilder.build(Unknown Source)
... 60 more
Run Code Online (Sandbox Code Playgroud)
假设我想从这个URL打开文件
https://www.filepicker.io/api/file/KW9EJhYtS6y48Whm2S6D?signature=4098f262b9dba23e4766ce127353aaf4f37fde0fd726d164d944e031fd862c18&policy=eyJoYW5kbGUiOiJLVzlFSmhZdFM2eTQ4V2htMlM2RCIsImV4cGlyeSI6MTUwODE0MTUwNH0=
Run Code Online (Sandbox Code Playgroud)
所以我做了类似的事情:
try{
URL URL = new URL('https://www.filepicker.io/api/file/KW9EJhYtS6y48Whm2S6D?signature=4098f262b9dba23e4766ce127353aaf4f37fde0fd726d164d944e031fd862c18&policy=eyJoYW5kbGUiOiJLVzlFSmhZdFM2eTQ4V2htMlM2RCIsImV4cGlyeSI6MTUwODE0MTUwNH0=');
String = path = "D://download/";
InputStream ins = url.openStream();
OutputStream ous = …Run Code Online (Sandbox Code Playgroud) 我正在寻找outputStream.write()能够接受一个int数组的东西.
实际上,我正在使用这个:outputStream.write()但是这个只接受byte,byte[]或者int.
我可以使用byte[]但我想发送的值是
[255,44,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255]
Run Code Online (Sandbox Code Playgroud)
所以我可以使用,byte[]因为范围只来自 -127 to 127:/
它是在Port_Com上发送一个命令,它只接受19个字节的数据包,并且必须以255开始和结束.
在我的情况下,输出流基本上是FileOutputStream.因此对于此代码:
ByteArrayOutputStream bos = (ByteArrayOutputStream) streamToEncrypt;
Run Code Online (Sandbox Code Playgroud)
(其中streamToEncrypt是OutputStream)获得此异常:
java.lang.ClassCastException: java.io.FileOutputStream cannot be cast to java.io.ByteArrayOutputStream
Run Code Online (Sandbox Code Playgroud)
我需要做的就是从这个输出流中获取字节数组.我无权访问此级别的文件.我只有这个输出流,我必须在将其推送到文件之前加密
使用Google Drive Rest Api,我尝试使用此功能下载广告图片.
我得到一个包含以下代码的文件列表:
List<String> fileInfo = new ArrayList<>();
FileList result = mService.files().list()
.setPageSize(10)
.setQ("mimeType='image/jpeg'")
.setFields("nextPageToken, files(id, name)")
.execute();
List<File> files = result.getFiles();
if (files != null) {
for (File file : files) {
fileInfo.add(String.format("%s (%s)\n",
file.getName(), file.getId()));
}
}
Run Code Online (Sandbox Code Playgroud)
它有效.然后,使用文件ID,我获得一个带有以下代码的OutputStream:
String fileId = "file_id";
OutputStream fOut = new ByteArrayOutputStream();
mService.files().export(fileId,"image/jpeg").executeMediaAndDownloadTo(fOut);
Run Code Online (Sandbox Code Playgroud)
现在,我如何将其转换为文件然后转换为位图?
在java 8中,以下之间有什么真正的区别:
try (OutputStream os = Files.newOutputStream(path)) {
[...]
}
Run Code Online (Sandbox Code Playgroud)
和
try (OutputStream os = new BufferedOutputStream(Files.newOutputStream(path))) {
[...]
}
Run Code Online (Sandbox Code Playgroud)
我阅读了这个SO question and answers,但它让我很困惑。
PS:Java 11 有什么变化?
我正在尝试进行此类转换,但我遇到了一些问题.
假设我有一个以下字符串:
String in = "1234567890123456";
Run Code Online (Sandbox Code Playgroud)
然后我将它转换为ByteArrayInputStream,如下所示:
ByteArrayInputStream bais = new ByteArrayInputStream(in.getBytes("UTF-8"));
Run Code Online (Sandbox Code Playgroud)
我也有:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Run Code Online (Sandbox Code Playgroud)
然后我加密:
ch.encrypt(bais, baos);
Run Code Online (Sandbox Code Playgroud)
所以现在我的"输出"了baos.当我这样做的时候:
byte[] b2 = baos.toByteArray();
int[] i2 = toUnsignedIntArray(b2);
writeIntegersAsHex(i2);
Run Code Online (Sandbox Code Playgroud)
哪里(我知道它不是最优雅的方式,但它仅用于测试):
public static void writeIntegersAsHex(int[] integers) {
int height = integers.length;
for (int i = 0; i < height; i++) {
System.out.print(Integer.toHexString(integers[i]) + ",");
}
System.out.println("");
}
Run Code Online (Sandbox Code Playgroud)
我得到这样的输出:
d1,68,a0,46,32,37,25,64,67,71,17,df,ee,ef,2,12,
Run Code Online (Sandbox Code Playgroud)
并且该输出是正确的,因为当我处理包含相同字符串的文件时,in该输出是相同的.但我无法从中获得正确的字符串baos.
请不要问我为什么这样做,因为这不是我的电话.我是学生,这是一个例外.
算法(顺便说一句,它是aes128)工作正常,文件,但我不能得到字符串输入流和输出流到字符串工作正常.
我正在开发一个实现记录器功能的Python应用程序
这是我在官方网站上的代码:
import logging
#create logger
logger = logging.getLogger("simple_example")
logger.setLevel(logging.DEBUG)
#create console handler and set level to debug
fh = logging.RotatingFileHandler(MyFile, etc)
fh.setLevel(logging.DEBUG)
#create formatter
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s -
%(message)s")
#add formatter to fh
fh.setFormatter(formatter)
#add fh to logger
logger.addHandler(fh)
#"application" code
logger.debug("debug message")
logger.info("info message")
logger.warn("warn message")
logger.error("error message")
logger.critical("critical message")
Run Code Online (Sandbox Code Playgroud)
这是文件中的输出:这是完美的
2005-03-19 15:10:26,618 - simple_example - DEBUG - 调试消息
2005-03-19 15:10:26,620 - simple_example - INFO - info message
2005-03-19 15:10:26,695 - …
我想从文本文件中读取一些数据.这是文本文件中数据的格式:
A,20, ,0
B,30, ,0
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
public ArrayList rechercherSalle()
{
String nom;
String ligne;
ArrayList<Salle> listeSalles = new ArrayList<Salle>();
Salle salle = new Salle();
try {
InputStream flux = new FileInputStream("salle.txt");
InputStreamReader lecture = new InputStreamReader(flux);
BufferedReader buff = new BufferedReader(lecture);
ligne = buff.readLine();
while (ligne != null) {
String[] objetSalle = ligne.split(",");
nom = objetSalle[0];
String capacite_maxString = objetSalle[1];
Integer capacite_max = Integer.parseInt(capacite_maxString);
String capacite_actuelleString = objetSalle[3];
Integer capacite_actuelle = Integer.parseInt(capacite_actuelleString);
String proprietaire = objetSalle[2];
salle = new Salle(); …Run Code Online (Sandbox Code Playgroud) 我想输出一个int32位二进制格式.循环和转移我唯一的选择?