Tag: stream

JAVA-生成预定的JSON

我从这开始: http : //examples.javacodegeeks.com/enterprise-java/rest/jersey/json-example-with-jersey-jackson/并在localhost上创建了一个JSON:8080 ……我不确定是否有是每3或5秒生成新Json的任何方法。 例如,每3秒只向新的JSON添加一个数字。 @Path(“/LNU.se”) public class EntryPoint { @GET @Path(“get”) @Produces(MediaType.TEXT_PLAIN) public String test() { return “mahdi Test”; } @POST @Path(“post”) @Consumes(MediaType.TEXT_PLAIN) public Response postTest(Track track){ String resault = ” the track is saved mahdi: “+ track; return Response.status(201).entity(resault).build(); } } 和另一课: import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; public class App { […]

Netty ByteBuf在解码时不会获得大输入的完整消息

有一个遗留应用程序通过TCP执行json。 我使用netty服务器,我想通过Gson将json转换为Pojo。 为了做到这一点,我转换创建了一个Decoder,它将ByteBuf作为输入并创建My Object。 问题是Bytebuf的大小为1024,json比那个大得多(> 20mo) @Sharable public class RequestDecoder extends MessageToMessageDecoder { @Override protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List out) throws Exception { InputStream json = new ByteBufInputStream(msg); InputStreamReader inputStreamReader = new InputStreamReader(json, Charsets.UTF_8); JsonReader reader; reader = new JsonReader(inputStreamReader); reader.setLenient(true); Request object = new Gson().fromJson(reader, Request.class); try { reader.close(); } catch (IOException […]

使用Jasper和.xlsx的内容消息不可读

我有以下代码将xlsx导出到流。 它工作,我得到xlsx文件,但它说excel在“…”中发现不可读的内容,并询问我是否要恢复工作簿的内容。 当我这样做时,我会在正确的位置使用正确的数据获取xlsx文件,如我所愿。 如何避免或抑制此错误? try { ServletOutputStream servletOutputStream = resp.getOutputStream(); JasperReport jasperReport = JasperCompileManager .compileReport(path + “Template/Instructions_with_CHGFOX_Template/testeXlsx2.jrxml”); JasperPrint print = JasperFillManager.fillReport(jasperReport, parameters, new JRBeanCollectionDataSource(list)); JRXlsxExporter xlsxExporter = new JRXlsxExporter(); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); xlsxExporter.setParameter(JRExporterParameter.JASPER_PRINT, print); xlsxExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, title + “.xlsx”); xlsxExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, servletOutputStream); xlsxExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outStream); xlsxExporter.exportReport(); resp.setContentType(“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”); resp.setHeader(“Content-Disposition”, “attachment; filename=” + title + “.xlsx”); servletOutputStream.write(outStream.toByteArray()); resp.getOutputStream().write(outStream.toByteArray()); […]

使用ASCII线处理Java IO的最快方法

我正在通过Socket使用ASCII输入/输出流,速度至关重要。 我听说使用正确的Java技术确实有所作为。 我有一本教科书说使用Buffers是最好的方法,但也建议使用DataInputStreamReader进行链接。 对于输出我使用带有OutputStreamWriter的BufferedOutputStream似乎没问题。 但我不确定输入流的用途。 我正在开发新系列,扫描仪有用吗? 速度至关重要,我需要尽快从网络上获取数据。 谢谢。 PH

如何在java 8中使用流将集合/数组转换为JSONArray

我有一个双数组,我需要使用java流将数组转换为JSONArray。 我尝试使用forEach(共享可变性),这会导致数据丢失。 public static JSONArray arrayToJson(double[] array) throws JSONException{ JSONArray jsonArray = new JSONArray(); Arrays.stream(array) .forEach(jsonArray::put); return jsonArray; } 有什么方法可以使用流创建JSONArray吗?

关闭I / O流

当我不关闭流时会发生什么坏事? 关闭操作是否自动冲洗? 程序退出后是否所有流都关闭了? 提前致谢。

在JavaFX中将SLF4J日志重定向到TextArea

我想在JavaFX中的TextArea中显示SLF4J记录的错误。 到目前为止我所拥有的是logback-test.xml中的一个appender : %-4relative [%thread] %-5level %logger{35} – %msg%n TextArea准备好接收流: public class Output extends OutputStream{ private final TextArea ta; public Output(TextArea ta) { this.ta = ta; } @Override public void write(int b) throws IOException { if (ta!=null) { ta.appendText(String.valueOf((char) b)); } } } 和一个处理追加的类: public class AppTA extends AppenderBase { PatternLayoutEncoder encoder; OutputStream os; @Override […]

使用超大的json文件。 总是内存不足

如何使用带有jackson的流API json? 请参阅下面的代码: ObjectMapper mapper = new ObjectMapper(); Map map = new HashMap(); List list = new ArrayList(); // Get images in database try { Class.forName(DRIVER); connection = DriverManager.getConnection(URL, USER, PASSWORD); Statement s = connection.createStatement(); ResultSet r = s.executeQuery(“select * from images”); while (r.next()) { byte[] imageBytes = r.getBytes(“image”); String imageBase64 = DatatypeConverter.printBase64Binary(imageBytes); list.add(imageBase64); } […]

PrintWriter vs PrintStream vs OutputStreamWriter timecosts

如您所知,我们在java中有几个用于将数据写入流的工具。 在此示例代码中,我通过运行时对它们进行了比较 有人可以解释一下吗? 谢谢。 这是代码: import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.io.PrintStream; import java.io.PrintWriter; public class IOtests { public static void main(String[] args) throws Exception { char[] chars = new char[100]; byte[] bytes = new byte[100]; for (int i = 0; i < 100; i++) { chars[i] = (char) i; bytes[i] = (byte) i; } OutputStreamWriter […]

Hibernate 4.2.2从未知长度的输入流创建blob

嗨,我想从输入流创建一个blob in hibernate,但我不知道流的长度。 Hibernate.getLobCreator(sessionFactory.getCurrentSession()).createBlob(stream, length) 如何在不知道流的长度的情况下创建一个blob? EDIT1 在较旧的hibernate版本中它是可能的 http://viralpatel.net/blogs/tutorial-save-get-blob-object-spring-3-mvc-hibernate/ Blob blob = Hibernate.createBlob(file.getInputStream()); EDIT2 好吧,但它有一个错误的实施 返回新的SerializableBlob(new BlobImpl(stream,stream.available())); stream.available不是真正的大小 编辑3 我试过了 session.doWork(new Work() { @Override public void execute(Connection conn) throws SQLException { LargeObjectManager lobj = ((org.postgresql.PGConnection) conn).getLargeObjectAPI(); 但是conn只是来自c3p0的NewProxyConnection。