我正在用struts创建一个项目,我在使用Jasper IReports时遇到了问题.我想将一些信息导出到pdf文件中并且我一直得到java.lang.IllegalStateException:getOutputStream()已被调用...由于在页面已打开PrintWriter时在我的代码中打开ServletOutputStream而导致异常.
代码在模型中(因此它不在jsp中,它在java文件中),如下所示:
public void handle(HttpServletResponse res, Connection connection, String path)throws Exception{
ServletOutputStream out = null;
try {
JasperDesign jasperDesign = JRXmlLoader.load(path);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
byte[] bytes = JasperRunManager.runReportToPdf(jasperReport, null, connection);
res.setContentType("application/pdf");
res.setContentLength(bytes.length);
out = res.getOutputStream();
out.write(bytes, 0, bytes.length);
} catch (Exception e) {
e.printStackTrace();
} finally {
out.flush();
out.close();
}
Run Code Online (Sandbox Code Playgroud)
我检查了连接,路径和HttpServletResponse,都运行正常.
我是Jasper Reports的新手以及编写PDF格式的东西,所以你可以 - 正确地 - 我对我在这里做的事情有一点了解,显然我的代码是通过网络从某处复制/粘贴的.
我曾尝试使用PrintWriter而不是OutputStream,将字节转换为String并使用PrintWriter.append(String)方法(allthought不是String是CharSequence),但它不会将数据提取到PDF中.
我也尝试获取PrintWriter,关闭它以打开OutputStream(不起作用)或刷新它(两者都没有).
任何帮助解决方案使用任何可以显示pdf数据的解决方案都会很棒.非常感谢!
我正在使用以下代码从连接到服务器关闭InputStream和OutputStream:
try {
if (mInputStream != null) {
mInputStream.close();
mInputStream = null;
}
if (mOutputStream != null) {
mOutputStream.close();
mOutputStream = null;
}
} catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
然而,溪流没有关闭,他们仍然活着.如果我再次连接,则有两个不同的InputStream.该catch部分没有例外.
我究竟做错了什么?
我正在尝试在两部Android 4.0.1手机之间发送数据,而当我发送数据时,我在另一侧没有收到完整的阵列。
我的代码:
byte[] buffer = new byte[4096];
int bytes;
bytes = inputStream.read(buffer);
// bytes = 1008
// buffer[1008..4095] = 0
// =================== other side =============================
byte[] message = Tools.concatByteArray(response, authorizationMessage);
// Debug tells me that the array has 1400 bytes
outputStream.write(message);
outputStream.flush();
Run Code Online (Sandbox Code Playgroud)
调试中的消息是:
[102, -32, 27, 23, 31, 111, -43, 124, 105, -122, 89, 13, 80, 62, 107, -21, 44, -93, -24, 100, -28, 18, -10, -55, 8, -7, 95, -82, -127, -125, -13, -14, 82, 93, -112, 33, …Run Code Online (Sandbox Code Playgroud) 当用户点击按钮时,客户端浏览器上的windchill GUI应该在他的系统上下载特定的pdf文件.我已经通过使用以下代码实现了这一点.
<body>
<%
String pdfname= session.getAttribute("pdfname").toString();
String Pdfpath= session.getAttribute("pdfpath").toString();
File f =new File(Pdfpath);
Boolean flag=false;
if(f.exists())
{
BufferedInputStream filein = null;
BufferedOutputStream out2=null;
try {
File file = new File(Pdfpath);//specify the file path
byte b[] = new byte[1048576];
int len = 0;
filein = new BufferedInputStream(new FileInputStream(file));
out2=new BufferedOutputStream(response.getOutputStream());
response.setHeader("Content-Length", ""+file.length());
response.setContentType("application/pdf");
response.setHeader("Content-Disposition","attachment;filename="+pdfname);
response.setHeader("Content-Transfer-Encoding", "binary");
while ((len = filein.read(b)) > 0) {
out2.write(b, 0, len);
out.println("Your Pdf Document Is Generated Please close it");
}
filein.close();
out2.flush();
out2.close();
}
catch(Exception …Run Code Online (Sandbox Code Playgroud) 我知道在编写文本时应该使用编写器而不是输出流,但我仍然不明白为什么outputStream.txt在运行此程序后文件中有额外的字符:
public static void main(String[] args) throws FileNotFoundException, IOException
{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("C:\\Data\\tmp\\outputStream.txt")));
oos.writeObject("SomeObject");
oos.writeUTF("SomeUTF");
oos.flush();
oos.close();
BufferedWriter writer = new BufferedWriter(new FileWriter(new File("C:\\Data\\tmp\\outputWriter.txt")));
writer.write("SomeObject");
writer.write("SomeUTF");
writer.flush();
writer.close();
}
Run Code Online (Sandbox Code Playgroud)
outputWriter.txt正如预期的那样,该文件是17字节,但是outputStream.txt是28,包括一些无法识别的文本.这是为什么?
我有一个Java工具,代码如下:
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
// any error message?
StreamGobblerLocal errorGobbler = new StreamGobblerLocal(proc.getErrorStream(), "ERROR");
// any output?
StreamGobblerLocal outputGobbler = new StreamGobblerLocal(proc.getInputStream(), "OUTPUT");
// kick them off
errorGobbler.start();
outputGobbler.start();
// any error???
int exitVal = proc.waitFor();
commandResultBean.setCLI_ReturnValue(exitVal);
Run Code Online (Sandbox Code Playgroud)
它可以工作,但是当子进程的输出很大时,它会死锁.我几乎可以肯定问题在于:
由于某些本机平台仅为标准输入和输出流提供有限的缓冲区大小,因此无法及时写入输入流或读取子进程的输出流可能导致子进程阻塞甚至死锁.
我在这里找到了它:http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html
我该如何解决这个问题?记事本中的大输出就像30000行,我必须记录这些行.
我的代码是从本文中复制的,因此您可以检查我在此处未复制的实现.
http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html
请注意,我不是该工具的第一个开发人员,所以我很难回答诸如"你为什么使用这个"或类似问题的问题.
任何帮助表示感谢,提前谢谢.
我正在向蓝牙插座发送一个字节数组,我从蓝牙打印机得到响应,但我没有得到正确的图像数据发送确认.
我outputstream按以下方式编写字节数组:
byte[] queryData = new byte[]{
0x1B, 0x2A, 0x43, 0x41,
0x00, 0x00, 0x00, 0x00,
0x01, 0x27, 0x5E,
0x01, 0x00, 0x00, 0x00, 0x00
};
outputStream.write(queryData);
outputStream.flush();
Run Code Online (Sandbox Code Playgroud)
还有其他方法可以写二进制数据outputstream吗?
我被卡住并打开了任何建议.
android bluetooth outputstream bluetooth-profile bluetooth-socket
我有一个包含几个cout语句的C++程序.我确保所有这些都以一个结尾endl.我的问题是程序很少停止,直到用户按下Enter(所以我假设输出缓冲区并不总是按照它应该刷新).按Enter键恢复程序执行.这是非常有问题的,因为我的程序需要几个小时才能执行,因此我无法一直按下输入!请注意,有时程序会在一分钟后停止,有时会在一个多小时后停止.
这是一个小代码片段:
for(int i = 0; i < _numIterations; i++){
std::cout << "Iteration " << i << std::endl;
// Computations and more print statements.
}
Run Code Online (Sandbox Code Playgroud)
请注意,我通过嵌入Python使用Theano,我的Python代码也包含print语句.我的Python代码只调用print,而不是sys.stdout.flush()每次打印后.但是,在Python生成的print语句之后,程序执行很少挂起.我错过了一些明显的事吗?我应该sys.stdout.flush()在Python代码中调用吗?不幸的是,我无法提供更多代码,因为我的程序包含数十个类.
[编辑]我挂了一个调试器时暂停了程序,没有可用的源显示.调用堆栈是:

似乎线程正在等待.但是,我自己没有设置这些线程.它们要么是由Cuda生成的,要么是我正在使用的Havok物理引擎.我会调查.
这是我的客户端/服务器的流程.
套接字在主线程中创建.
close()如何检测套接字已关闭的问题?
try {
os = new DataOutputStream(this.socket.getOutputStream());
os.write(data);
os.flush();
System.out.println("Written " + data.length + " bytes");
} catch (IOException e) {
System.out.println("Client failed to write to stream");
System.out.println(e.getMessage());
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
永远不会抛出异常.它说Written 60 bytes.有任何想法吗?
UPDATE
这是我阅读回复的方式.我等待数据,读取前4个字节(给出响应的长度),并继续读取,直到我读取指定的长度.这个循环永远不会结束,因为没有数据进入.
is = new DataInputStream(this.socket.getInputStream());
while(true){
while(is.available() > 0){
bos.write(is.read());
}
if(contentLength == 0 && bos.size() > 3){
byte[] bytes = bos.toByteArray();
byte[] size = Arrays.copyOf(bytes, 4);
contentLength = ByteBuffer.wrap(size).getInt(); …Run Code Online (Sandbox Code Playgroud) 我有一个正在创建文件的问题,但是这正在创建空文件。
我正在使用Dropbox的API,Dropbox的代码运行良好,但是我不知道我没有问题。我已经为我的应用程序使用了2º和3º代码,这运行良好。
这按层次操作。我正在发送outputStream功能。但这是空的。
我正在使用outputStream,因为我需要它与outputstream一起运行。
1º代码(Class Test || Call):
File tempFile=new File("C:\\PRUEBAS_TFG\\cloud.png"); if ( ! tempFile.exists() ) { tempFile.createNewFile(); }
File file = new File("C:\\PRUEBAS_TFG\\cloud.png");
OutputStream outputStream = new FileOutputStream(file);
catmanager.downloadFilesToItem(catmanager.getAllListCatalog().get(0), catmanager.getAllListCatalog().get(0).getItems().get(2), listFile, outputStream);
outputStream.close();
Run Code Online (Sandbox Code Playgroud)
2º代码(类别管理员||1ºbody):
public void downloadFilesToItem(Catalog catalog, Item item, List<String> files, OutputStream output){
try{
String fsPath;
fsPath = pathCatalog(catalog);
getFSManager().changeDirectoryConfigNPath(fsPath+"/"+item.getName()+"_item");
for(int i = 0;i<files.size();i++){
getFSManager().downloadFile(files.get(i), output);
}
}catch(Exception e){
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
3ºCode(FSManager类||2ºbody)
public void downloadFile(String fileName, OutputStream output) throws IOException{//Aqui deveria Buscar el Fichero Funciona por que …Run Code Online (Sandbox Code Playgroud)