请教:如何将String里边的内容按原格式写入文件

在做加密解密程序
首先从文件里读取内容 加密后存进另一文件
后从该文件里读取 解密后放进一个String里(控制台输出格式正常) 然后写到另一文件里
写进后发现该文件里没了换行 几段内容乱成一体了
如何才能保持远格式写入呢?

我用的
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"GBK"));

由于是从加密文件里解密出来的String 是一个整体 不是从文件里读一行写一行

6个回答

还是上次的那个问题么?

你读原文件的时候readline,这是读一行,然后在后面加了\n,问题就出在这,原来一行是以\r\n结尾的,你readline过后,\r\n没了,而你只手动加上了\n,这样\r就丢了..

所以你可能看到原文件是好的,但是经过加密解密过后内容都是对的,但是不换行了.

代码我回在那个问题里了,这里就不再贴一遍了.

你不要用字符流,改用字节流试试。
String text = "解密之后的字符串";
File file = new File("写文件位置和文件名");
OutputStream bos = new FileOutputStream(file);
bos.write(test.getBytes());
bos.colse();
这样应该就没有问题了。

话说,楼主使用记事本打开的文件吧?
这个是记事本的问题,\n不会换行,楼主可以用写字板、word神马的打开新生成的文件,就会看到正常的格式了……

我相信楼主一定用错了加密解密的API,
通常,加解密是针对字节流的,不是针对字符流的。
你顺着这个思路去走,试试看。

BTW, 能把你加解密的API声明帖出来看看吗?

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"GBK"));

[quote]你说得对 的确是这样
有没有办法让记事本里也正常显示呢[/quote]
既然是文本加解密,
在解密之后,对"\n"使用"\r\n"进行替换,就行了。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
请教:Spring + Hibernate 无法将数据写入数据库?
Spring + Hibernate 无法将数据写入数据 **请教: 通过Junit单元测试Service可以将数据写入数据库;但部署访问却无法向数据库写入数据。** 1 环境: Spring 3.1.2 Hibernate 4.1.4 Jdk1.6 2 配置: 2.1 Web.xml <!-- Spring ApplicationContext配置文件的路径,可使用通配符,多个路径用,号分隔 此参数用于后面的Spring Context Loader --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--Dispathcer Servlet --> <servlet> <servlet-name>spring-mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Spring MVC Servlet 拦截.do结尾的请求--> <servlet-mapping> <servlet-name>spring-mvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- Filter 定义 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--openSessionInView--> <filter> <filter-name>openSessionInView</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>sessionFactoryBeanName</param-name> <param-value>sessionFactory</param-value> </init-param> </filter> <filter-mapping> <filter-name>openSessionInView</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 2.2 applicationContext.xml <!--扫描并自动装配 --> <context:annotation-config /> <context:component-scan base-package="com.sp" /> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:application.properties</value> </property> </bean> <!-- 数据源配置 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <!-- Connection Info --> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- Connection Pooling Info --> <property name="maxActive" value="${dbcp.maxActive}" /> <property name="maxIdle" value="${dbcp.maxIdle}" /> <property name="defaultAutoCommit" value="false" /> <!-- 连接Idle一个小时后超时 --> <property name="timeBetweenEvictionRunsMillis" value="3600000" /> <property name="minEvictableIdleTimeMillis" value="3600000" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.connection.autocommit">true</prop> <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> <property name="packagesToScan"> <list> <value>com.sp.dao</value> <value>com.sp.entity.dict</value> </list> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" abstract="false" lazy-init="default" autowire="default"> <property name="sessionFactory"><ref bean="sessionFactory" /></property> </bean> <!-- 事务管理配置 --> <!--<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="insert*" read-only="false" propagation="REQUIRED" /> <tx:method name="get*" read-only="false" propagation="REQUIRED" /> <tx:method name="add*" read-only="false" propagation="REQUIRED" /> <tx:method name="update*" read-only="false" propagation="REQUIRED" /> <tx:method name="delete*" read-only="false" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <aop:config> <aop:advisor pointcut="execution(* com.sp.service.*.*(..))" advice-ref="txAdvice"/> </aop:config> </beans> 2.3 spring-mvc.xml <!-- 启动注解驱动的SpringMVC功能,注册请求URL和注解POJO类方法的映射 --> <mvc:annotation-driven /> <!-- 自动扫描且只扫描@Controller --> <context:component-scan base-package="com.sp.web" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <context:component-scan base-package="com.sp.dao.impl"></context:component-scan> <context:component-scan base-package="com.sp.service.impl"></context:component-scan> <mvc:default-servlet-handler /> <!-- 对模型视图名称的解析,在请求时模型视图名称添加后缀。定义JSP --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 支持上传文件 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" /> 2.4 hibernate.cfg.xml <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/test</property> <property name="connection.username">root</property> <property name="connection.password">password</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect <property name="dialect">org.hibernate.dialect.MySQLDialect</property> --> <!-- Enable Hibernate's automatic session context management --> <!-- <property name="current_session_context_class">thread</property> --> <!-- Disable the second-level cache --> <!-- <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>--> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <property name="format_sql">true</property> <!-- Drop and re-create the database schema on startup--> <property name="hbm2ddl.auto">update</property> <property name="javax.persistence.validation.mode">none</property> 3 代码: 3.1 Service层: @Component("userService") public class UserServiceImpl implements UserService{ @Autowired private UserDao userDao; @Autowired private FormValidator validator; @Override public String userLogin(UserModel user) { String password = userDao.userLogin(user); if(password.equals(user.getPassword())){ return "sucess"; }else{ return "false"; } } @Override public List<ProjectType> getAllProjectTypes(){ List<ProjectType> projectType = userDao.getAllProjectTypes(); return projectType; } @Override public String addProjectType(@ModelAttribute("newProjectType")ProjectType projectType,BindingResult result,SessionStatus status){ validator.validate(projectType, result); if(result.hasErrors()){ return "newProjectType"; } userDao.save(projectType); //status.setComplete(); return "redirect:showProjectTypes.do"; } //添加《项目类型》 @Override public String addProjectType(ProjectType projectType) { userDao.save(projectType); return "redirect:showProjectTypes.do"; } } 3.2 Control层 @Controller @RequestMapping(value = "/user") public class UserController { @Autowired private UserService userService; @Autowired private FormValidator validator; @RequestMapping(value = "/login",method=RequestMethod.POST) protected String handle(UserModel user,BindingResult result, Model model){ String flag = userService.userLogin(user); System.out.println("flag:" + flag); if ("sucess".equals(flag)){ return "sucess"; } return "test1"; } @RequestMapping(value = "/showProjectTypes") protected ModelAndView getAllProjectTypes(){ ModelAndView mav = new ModelAndView("showProjectTypes"); List<ProjectType> projectTypes = userService.getAllProjectTypes(); mav.addObject("SEARCH_PROJECTTYPE_RESULTS_KEY", projectTypes); return mav; } @RequestMapping(value = "/addProjectType",method=RequestMethod.GET) protected ModelAndView newProjectTypeForm(){ ModelAndView mav = new ModelAndView("newProjectType"); ProjectType projectType = new ProjectType(); mav.getModelMap().put("newProjectType", projectType); return mav; } @RequestMapping(value = "/saveProjectType",method=RequestMethod.POST) protected String addProjectType(@ModelAttribute("newProjectType")ProjectType projectType,BindingResult result,SessionStatus status){ //return userService.addProjectType(projectType, result, status); return userService.addProjectType(projectType); } } 3.3 DAO层 @Component("userDao") public class UserDaoImpl implements UserDao{ @Autowired private SessionFactory sessionFactory; public ProjectType getById(int id) { return (ProjectType) sessionFactory.getCurrentSession().get(ProjectType.class, id); } @Override public String userLogin(UserModel user) { return "123456"; } //获取数据 @Override @SuppressWarnings("unchecked") public List<ProjectType> getAllProjectTypes() { Criteria ceriteria = sessionFactory.getCurrentSession().createCriteria(ProjectType.class); return ceriteria.list(); } //新增数据; @Override // @Transactional(readOnly = true, propagation = Propagation.REQUIRED) public int save(ProjectType projectType) { System.out.println("save:---"); System.out.println(projectType.getIndex()); System.out.println(projectType.getProjectType()); System.out.println(projectType.getProjectTypeAbbr()); return (Integer) sessionFactory.getCurrentSession().save(projectType); } }
新手请教java文件操作的一个小问题~十分感谢
这个类的功能是将任意一个二进制文件中的0压缩成0的数目+0的形式 即:将二进制文件中16进制显示的00压缩成 "0的数目+00的形式" 如00 00 00 1A 压缩成03 00 1A 待压缩文件16进制内容: ![图片说明](https://img-ask.csdn.net/upload/201603/15/1458037363_273551.png) 代码执行后后生成的文件却成了如图所示的情况 ![图片说明](https://img-ask.csdn.net/upload/201603/15/1458037372_804471.png) 求教什么地方出错了,万分感谢。 ``` import java.io.*; public class FileCompression { public static void main(String args[]) throws Exception { FCompression(); } //将二进制文件中16进制显示的00压缩成 "0的数目+00的形式" //如00 00 00 1A 压缩成03 00 1A public static void FCompression()throws Exception { File dFile=new File("E:/Picture Collections/dest.cpac");//压缩生成目标文件 File file = new File("E:/Picture Collections/dest.pac");//待压缩文件 FileInputStream fin=new FileInputStream(file); BufferedInputStream bin=new BufferedInputStream(fin); PushbackInputStream pbin=new PushbackInputStream(bin); //DataInputStream din=new DataInputStream(fin); FileOutputStream fout = new FileOutputStream(dFile,true); DataOutputStream dout=new DataOutputStream(fout); byte[] bt=new byte[1]; int len=0; int i=0;//i代表00 的数目 1byte=2个16进制位 int temp; while( (len = fin.read(bt)) != -1 ) { if (bt[0]==0&&(temp=pbin.read())==0)//若读取的数和它后的数字都为0则跳过 { pbin.unread(temp);//推回预读的下一位 i++; //调试语句System.out.println("--"+i); } else if (bt[0]==0&&(temp=pbin.read())!=0)//若读取的数为0,但它后的数不为0 { pbin.unread(temp); i++; //调试语句System.out.println("+++++++"); dout.writeByte(i);//写入00 的个数 dout.writeByte(0);//写入0 i=0;//重置i } else { //调试语句System.err.println("----"); fout.write(bt,0,len); } } dout.close(); pbin.close(); System.out.println("Compress successfully"); } } ```
请教,ObjectOutputStream写入到网络中报错!
我使用ObjectOutputStream从客户端写入对象到网络中,在服务器端读取对象, 然后保存到文件中。但是当我把对象写入到网络中时,出现异常。 client.java ``` public class Client<M> { public void add(M m) {//向远程网络中存储对象 Socket socket; ObjectOutputStream oos = null; try { try{ socket = new Socket("127.0.0.1", 8011); OutputStream os = socket.getOutputStream(); oos = new ObjectOutputStream(os); oos.writeObject(m); oos.flush(); }finally { if(oos!=null){ oos.close(); } } } catch (Exception e) { e.printStackTrace(); } } } ``` Server.java ``` public class Server { public static void main(String[] args) { new Server().startServer(); } public void startServer(){ ServerSocket ss; ServerThread st; try { ss = new ServerSocket(8011); while(true){ System.out.println("服务已经开启"); Socket socket = ss.accept(); st = new ServerThread(socket);//创建线程对象 ExecutorService es = Executors.newCachedThreadPool(); es.execute(st); //开启线程 es.shutdown(); socket.close();//关闭socket } } catch (IOException e) { e.printStackTrace(); } } } ``` 线程 ServerThread.java ``` public class ServerThread implements Runnable{ private Socket socket; //private ObjectOutputStream oos = new ObjectOutputStream(); private FileOutputStream fos; private ObjectInputStream ois; public ServerThread(Socket socket) { this.socket = socket; } @Override public void run() { try { InputStream is = socket.getInputStream(); ois = new ObjectInputStream(is); Object obj = ois.readObject();//读取网络中的对象 System.out.println(obj); } catch (Exception e) { e.printStackTrace(); }finally { try { if(ois!=null) ois.close(); } catch (IOException e) { e.printStackTrace(); } } } } ``` ## 错误如下: ``` 通过客户端向服务器写入对象 java.net.SocketException: Software caused connection abort: socket write error at java.net.SocketOutputStream.socketWrite0(Native Method) at java.net.SocketOutputStream.socketWrite(Unknown Source) at java.net.SocketOutputStream.write(Unknown Source) at java.io.ObjectOutputStream$BlockDataOutputStream.drain(Unknown Source) at java.io.ObjectOutputStream$BlockDataOutputStream.flush(Unknown Source) at java.io.ObjectOutputStream.flush(Unknown Source) at java.io.ObjectOutputStream.close(Unknown Source) at com.fan.client.Client.add(Client.java:21) at com.fan.bean.BaseStore.save(BaseStore.java:22) at com.fan.execute.StudentExecute.studentExecute(StudentExecute.java:32) at com.fan.execute.StuSelectionCourseSys.startview(StuSelectionCourseSys.java:32) at com.fan.test.Test.main(Test.java:11) ``` 希望同志们能给予我解答,十分感谢!!!!
c#FileStream复制视频文件问题
文件不是只读的,我能手动删除,但运行出错说没有权限,请教各位哪里出了问题 代码如下![图片说明](https://img-ask.csdn.net/upload/201703/30/1490860658_832271.jpg) static void Main(string[] args) { //文件复制过程:先将要复制的多媒体文件读取出来 //然后再写入到指定的位置 string source = @"F:\音标\謝孟媛英语音标发音01_标清.flv"; //string source = @"C:\Users\kaka\Desktop\网站.txt"; string target = @"C:\Users\kaka\Desktop"; CopyFile(source, target); Console.WriteLine("复制成功"); } public static void CopyFile(string source, string target) { //创建一个读取的流 using(FileStream fsRead = new FileStream(source,FileMode.Open,FileAccess.Read)) { //创建一个写入的流 using (FileStream fsWrite = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write)) { byte[] buffer = new byte[1024 * 1024 * 5]; //因为文件可能比较大,所以我们通过循环去读取 while (true) { //返回本次读取实际的字节数 int r = fsRead.Read(buffer, 0, buffer.Length); //如果r = 0 时候,就证明什么都读取不到,已经读取完了 if (r == 0) { break; } //因为r是实际读取到的,所以写入时候用r fsWrite.Write(buffer, 0, r); } } } }
请教一个JVM内存优化的问题
<p>自己写的一个WEB爬虫程序,程序方面的优化我自己感觉做的不错了,各种对象的使用都想办法不让内存浪费。 <br />但还是吃内存吃的很厉害 <br />我想主要的消耗在于,对于每一个爬下来的网页,都要变成String在内存中分析超链接,这个估计很大。 <br />另外对于每一个分析到的URL,都需要保存起来。 <br /><br /><br />我当前使用的内存优化参数如下 <br />-Xms1024M -Xmx1024M -Xmn512M -Xss128K -XX:+UseParallelGC -XX:ParallelGCThreads=20 -XX:+UseParallelOldGC -XX:MaxPermSize=128M -XX:NewSize=128M -XX:MaxNewSize=128M -XX:SurvivorRatio=8 <br /><br />机器内存是1G。。上面那堆参数,除了最大最小堆内存我也是不求甚解,不是非常明白我设置了什么。网上搜答案也是五花八门,希望大家能推荐一个内存优化参数的文章 <br />另外我上面设置的参数有哪些方面可以优化,或者哪些地方写的不对。 <br /><br />核心的代码我都贴到下面:(我是刚入门的菜鸟,大家别笑话我写的差哦) <br /><br />这个是主类,负责控制,那个monitor大家就当做System.out.println,其实就是</p> <pre name="code" class="java">package com.wood.core; import java.util.concurrent.*; import java.util.*; import com.wood.core.monitor.*; public class CrawlerController implements Runnable { //一个类,用来管理URL,由各抓取线程共享 private URLMap urlmap; //已成功处理URL总数 private int count; //目标完成任务总数 private int total; //管理所有的抓取线程,必要时进行关闭,由于抓取线程存在I/O读取,而I/0读取中的线程是不可中断的,需调用线程的cancel函数 private Crawler[] crawlers; //线程工厂,可以监视产生的线程工作状态 private CrawlerFactory factory; //线程数目 private int threadMax; //当前活动线程 private int threadActive; //线程执行器 private ExecutorService exec; //当系统关闭时取消 private boolean canceled; public CrawlerController(String seed) { this(seed,20,10000); } public CrawlerController(List&lt;string&gt; seeds) { this(seeds,20,10000); } public CrawlerController(String seed,int threadNum) { this(seed,threadNum,10000); } public CrawlerController(List&lt;string&gt; seeds,int threadNum) { this(seeds,threadNum,10000); } public CrawlerController(String seed,int threadNum,int total) { //初始化URLMap,保存抓取启始位置 urlmap=new URLMap(seed); init(threadNum, total); } public CrawlerController(List&lt;string&gt; seeds,int threadNum,int total) { urlmap=new URLMap(seeds); init(threadNum, total); } //构造 private void init(int threadNum,int total) { //初始完成URL总数为0 count=0; //设置目标完成总数 this.total=total; threadMax=threadNum; threadActive=threadNum; //初始化工作线程 crawlers=new Crawler[threadMax]; String id="Crawler "; for(int i=0;i&lt;threadmax;i++) crawlers[i]="new" crawler(id+(i+1),this,urlmap);="" factory="new" crawlerfactory();="" exec="Executors.newCachedThreadPool(factory);" canceled="false;" }="" 检查当前工作线程状态,并打印系统状态="" private="" boolean="" check()="" {="" if(canceled)="" return="" false;="" int="" count="getCount();" if(count=""&gt;total) { MonitorHolder.getMonitor().print("已抓取"+COUNT+"页面,完成目标任务"+total+"页面\n"); cancel(); return false; } threadActive=factory.getActive(); /*if(threadActive&lt;=0) { MonitorHolder.getMonitor().print("无活动工作线程,抓取任务提前结束\n"); cancel(); return false; }*/ MonitorHolder.getMonitor().print_status("统计信息:成功抓取"+COUNT+"页面,当前活动线程为"+threadActive+"个\n"); return true; } //结束抓取 public void cancel() { //调用每个抓取线程的离开方法 for(Crawler cw:crawlers) cw.cancel(); //销毁工厂 factory.destory(); exec.shutdownNow(); MonitorHolder.getMonitor().print("成功结束抓取工作,共抓取"+getCount()+"页面\n"); this.canceled=true; } public synchronized void count() { count++; } public synchronized int getCount() { return count; } public int getTotal() { return total; } public URLMap getMap() { return urlmap; } @Override public void run() { while(!Thread.currentThread().isInterrupted() &amp;&amp; !canceled) { try { MonitorHolder.getMonitor().print("初始化完毕\n"); MonitorHolder.getMonitor().print("开始抓取工作\n"); for(Crawler cw:crawlers) exec.execute(cw); int check_count=0; while (check()){ TimeUnit.SECONDS.sleep(5); check_count++; if(check_count==24) { //每2分钟把待处理URL队列打乱一次 urlmap.shuffle(); MonitorHolder.getMonitor().print("控制信息!!!待抓取URL顺序将打乱\n"); check_count=0; System.gc(); } } } catch (InterruptedException e) { MonitorHolder.getMonitor().print("抓取工作被中断\n"); cancel(); } } } public static void main(String[] args) { List&lt;string&gt; seeds=new ArrayList&lt;string&gt;(15); //问问的各个分类 seeds.add("http://wenwen.soso.com/"); seeds.add("http://wenwen.soso.com/z/c1879048192.htm"); seeds.add("http://wenwen.soso.com/z/c1090519040.htm"); seeds.add("http://wenwen.soso.com/z/c1627389952.htm"); seeds.add("http://wenwen.soso.com/z/c855638016.htm"); seeds.add("http://wenwen.soso.com/z/c1191182336.htm"); seeds.add("http://wenwen.soso.com/z/c1191182336.htm"); seeds.add("http://wenwen.soso.com/z/c620756992.htm"); seeds.add("http://wenwen.soso.com/z/c553648128.htm"); seeds.add("http://wenwen.soso.com/z/c385875968.htm"); seeds.add("http://wenwen.soso.com/z/c687865856.htm"); seeds.add("http://wenwen.soso.com/z/c16777216.htm"); seeds.add("http://wenwen.soso.com/z/c318767104.htm"); seeds.add("http://wenwen.soso.com/z/c150994944.htm"); seeds.add("http://wenwen.soso.com/z/c922746880.htm"); seeds.add("http://wenwen.soso.com/z/c83886080.htm"); CrawlerController controller=new CrawlerController(seeds,5,2000000); List&lt;string&gt; format=new ArrayList&lt;string&gt;(10); //问问回答问题的格式 format.add("http://wenwen.soso.com/z/q"); controller.getMap().addLimit(format); Thread thread=new Thread(controller); thread.start(); } } </pre> <p><br /></p> <pre name="code" class="java">package com.wood.core; import java.util.*; import java.util.concurrent.TimeUnit; import java.util.regex.*; import java.io.*; import java.net.*; import com.wood.core.monitor.*; public class Crawler implements Runnable { //抓取线程ID private String id; //控制器 private CrawlerController controller; //URL,供各线程共享 private URLMap urlmap; private boolean canceled; //保存抓取的网页内容,用于分析超链接 private String content; //字符集 private String charset; //匹配超链接的正则表达式 private String link_reg="&lt;a\\s+(.*?)href\\s*=\\s*\"?(.*?)[\"(\\s*?)](.*?)&gt;"; private Pattern link_pattern=Pattern.compile(link_reg,Pattern.CASE_INSENSITIVE); //匹配字符集的正则表达式 private String charset_reg="&lt;meta\\s+http-equiv=\"content-type\"\\s+content=\"text html;="" charset="(.*?)\&amp;quot;(.*?)"&gt; "; private Pattern charset_pattern=Pattern.compile(charset_reg, Pattern.CASE_INSENSITIVE); private Pattern dir_path_pattern=Pattern.compile("^\\w+$", Pattern.CASE_INSENSITIVE); //保存当前抓取的URL的主机 private String host; //生成待建立的文件夹名 private String host_path; //抓取网页的根目录 private String root_path="E:\\web"; private File root_dir=new File(root_path); //输入来自URL,输出到文件 private InputStreamReader in=null; private OutputStreamWriter out=null; //输入输出缓冲区 private char[] buf=new char[10240]; //网页内容缓冲区 private StringBuilder contentbuilder=new StringBuilder(1024*1024); //通过一个URL,下载网页内容 private void download(URL url) { in=null; out=null; //建立抓取文件 String urlfile=url.getFile(); if(urlfile.endsWith("/")) urlfile+="indexindex"; File file=new File(host_path,urlfile); File file_dir=new File(file.getParent()); if(!file_dir.exists()) file_dir.mkdirs(); MonitorHolder.getMonitor().print(id+"开始准备下载"+url.toString()+"\n"); try { //打开链接 URLConnection conn=url.openConnection(); //超时30秒 conn.setConnectTimeout(30000); conn.setDoOutput(true); //不是HTML不下载 /*String type=conn.getContentType(); if( (type==null)|| (!type.equals("text/html")) ) return;*/ //将网页内容的缓冲区清空 contentbuilder.setLength(0); in=new InputStreamReader(conn.getInputStream(),charset); out=new OutputStreamWriter(new FileOutputStream(file),charset); int len; //读取网页内容,并写入文件,保存到网页内容缓冲里面 while((len=in.read(buf, 0, 10240)) &gt;0) { out.write(buf, 0, len); //append可以减小系统损耗 contentbuilder.append(buf, 0, len); } out.flush(); //将网页内容缓冲区的内容读到content中,用于分析 content=null; content=contentbuilder.toString(); } catch (IOException e) { MonitorHolder.getMonitor().print("错误!!!"+id+"下载页面"+url.toString()+"错误\n"); } finally{ try { if(in!=null) in.close(); if(out!=null) out.close(); } catch (IOException e) { } in=null; out=null; } } public Crawler(String id,CrawlerController controller,URLMap urlmap) { this.id=id; this.controller=controller; this.urlmap=urlmap; this.charset="utf-8"; canceled=false; MonitorHolder.getMonitor().print(id+"就绪\n"); } //设置字符集,通过正则表达式获取字符集格式 //为了性能,该功能并未使用 private void setCharset() { Matcher matcher=charset_pattern.matcher(content); String CHARSET="GB2312"; if(matcher.find()) CHARSET=matcher.group(1).trim(); this.charset=CHARSET; try { content=new String(content.getBytes("GB2312"),CHARSET); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } //从网页内容中分析超链接 private List&lt;string&gt; retrieveLinks(URL url) { List&lt;string&gt; urls=new LinkedList&lt;string&gt;(); if(content==null) return urls; Matcher matcher=link_pattern.matcher(content); String link; String host_reg="http://"+host; String host_nowww; if(host.contains("www")) host_nowww=host.substring(host.lastIndexOf("w")+2); else { host_nowww=host; } while(matcher.find()) { //通过抓取第1组的内容 link=matcher.group(2).trim().toLowerCase(); if (link.length() &lt; 1) { continue; } //网页内部链接,忽略 if (link.charAt(0) == '#') { continue; } //发送邮件链接,忽略 if (link.indexOf("mailto:") != -1) { continue; } if (link.toLowerCase().indexOf("javascript") != -1) { continue; } //分析绝对地址或相对地址 if (link.indexOf("://") == -1){ if (link.charAt(0) == '/') //处理绝对地址 link = "http://" + host+ link; else if(link.startsWith("./")) link="http://" + host+ link.substring(1); else { String file = url.getFile(); String file_path=file.substring(0, file.lastIndexOf('/')); while(link.startsWith("../")) { link=link.substring(link.indexOf("/")+1); file_path=file_path.substring(0, file_path.lastIndexOf("/")); } link="http://"+host+file_path+"/"+link; } } int index = link.indexOf('#'); if (index != -1) { link = link.substring(0, index); } if(!urlmap.testHost(link)) continue; if(!urlmap.testLimit(link)) continue; urls.add(link); } return urls; } //设置主机并建立目录 private void setHost(String host) { this.host=host; this.host_path=root_path+"\\"+host; File host_dir=new File(host_path); if(!host_dir.exists()) host_dir.mkdirs(); } @Override public void run() { if(!root_dir.exists()) root_dir.mkdirs(); while(!Thread.currentThread().isInterrupted() &amp;&amp; !canceled &amp;&amp; (controller.getCount()&lt;controller.gettotal()) )="" {="" try="" {="" 获得一个待抓取的url,如果没有可用url,则进入阻塞状态,该方法调用是线程安全的="" string="" urlstring="urlmap.getURL();" monitorholder.getmonitor().print(id+"开始抓取"+urlstring+"\n");="" 建立url="" url="" url="new" url(urlstring);="" 设置主机="" sethost(url.gethost());="" 清空网页内容,并下贼="" content="null;" download(url);="" 未下载到内容="" if(content="=null)" {="" continue;="" }="" monitorholder.getmonitor().print(id+"抓取"+urlstring+"完毕,进行解析\n");="" setcharset();="" 分析超链接="" list&lt;string=""&gt; urls=retrieveLinks(url); //将分析到的超连接加入到待抓取的URL队列中,并将成功抓取数+1 urlmap.addAll(urls); controller.count(); MonitorHolder.getMonitor().print(id+"解析"+urlstring+"完毕,共计"+urls.size()+"个超链接,开始新任务\n"); content=null; TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { MonitorHolder.getMonitor().print(id+"被中断\n"); canceled=true; } catch (MalformedURLException e) { MonitorHolder.getMonitor().print(id+"报告:URL格式错误\n"); }catch (Exception e) { e.printStackTrace(); } } } //退出,由控制器调用,关闭所有底层I/O资源 public void cancel() { try { if(in!=null) in.close(); } catch (IOException e) { } try { if(out!=null) out.close(); } catch (IOException e) { e.printStackTrace(); } MonitorHolder.getMonitor().print(id+"停止工作\n"); canceled=true; } } </pre> <p> </p> <pre name="code" class="java">package com.wood.core; import java.net.MalformedURLException; import java.net.URL; import java.util.concurrent.*; import java.util.*; public class URLMap { //待抓取的URL队列 private LinkedList&lt;string&gt; URLQueue; //缓存解析出来的URL,使用Hash方便快速查找,&lt;k,false&gt;表示未抓取,&lt;k,true&gt;表示抓取结束 private HashMap&lt;string,boolean&gt; cachedURL; private HashSet&lt;string&gt; hosts; private HashSet&lt;string&gt; format_limit; //默认构造器,初始化队列,集合 private URLMap() { URLQueue=new LinkedList&lt;string&gt;(); cachedURL=new HashMap&lt;string,boolean&gt;(50000); hosts=new HashSet&lt;string&gt;(20); format_limit=new HashSet&lt;string&gt;(20); } public URLMap(List&lt;string&gt; seeds) { this(); for(String s:seeds) cachedURL.put(s, false); URLQueue.addAll(seeds); for (String string : seeds) { try { URL test=new URL(string); String host=test.getHost(); hosts.add(host); } catch (MalformedURLException e) { e.printStackTrace(); } } } public URLMap(String seed) { this(); cachedURL.put(seed, false); URLQueue.add(seed); try { URL test=new URL(seed); hosts.add(test.getHost()); } catch (MalformedURLException e) { e.printStackTrace(); } } //将解析出来的URL添加到URLMap中,如果有重复则忽略,URL有效性由外部保证,该方法是线程安全的 public synchronized void addURL(String url) throws InterruptedException { //如果该URL已存在,忽略该URL if(cachedURL.keySet().contains(url)) return; cachedURL.put(url, false); URLQueue.add(url); //有可用URL,唤醒所有阻塞线程 notifyAll(); } //将解析出来的URL添加到URLMap中,如果有重复则忽略,URL有效性由外部保证,该方法是同步的 public synchronized void addAll(List&lt;string&gt; urls) throws InterruptedException { for (String url : urls) { if(cachedURL.keySet().contains(url)) continue; cachedURL.put(url,false); URLQueue.add(url); } notifyAll(); } //从当前URL队列中获取一个URL,如果当前队列无可用URL,则该线程进入阻塞状态 public synchronized String getURL() throws InterruptedException { //该处进入阻塞 while(URLQueue.size()==0) wait(); //将其从队列中删除 String url=URLQueue.remove(); cachedURL.put(url, true); return url; } public boolean testHost(String host) { for(String host_allow:hosts) { if(host.contains(host_allow)) return true; } return false; } public void addHost(List&lt;string&gt; l) { hosts.addAll(l); } public void addLimit(List&lt;string&gt; l) { format_limit.addAll(l); } public boolean testLimit(String url) { if(format_limit.size()==0) return true; else { for(String s:format_limit) if(url.contains(s)) return true; } return false; } //将待抓取队列打乱 private int swap_check=0; public synchronized void shuffle() { swap_check++; int size=URLQueue.size(); if(size&lt;1000) { java.util.Collections.shuffle(URLQueue); swap_check=0; } else { if(size&gt;=1000 &amp;&amp; size&lt;5000) { if(swap_check==5) { java.util.Collections.shuffle(URLQueue); swap_check=0; } } else { if(swap_check==15) { java.util.Collections.shuffle(URLQueue); swap_check=0; } } } } } </pre> <p><br /><br />附件里是内存使用情况<br /><strong>问题补充</strong><br />主要占用内存的是char[] <br />我的策略是所有分析到的有效URL都存储在HashMap中 <br />但是几万个URL不至于占用大多数内存吧? <br />我的工作线程只有5个,设置的多了,一会就崩了 <br /><br />哪位高人路过,帮我看眼吧 <br />祝你新年好运~</p> <p> </p> <p>附件里是内存占用,缓存的URL总数是20000个,一个大概占用80B,程序内存总数是150MB</p> <p>我不知道到底是什么东西不停的吃内存,回收不掉,程序运行的时间越长,线程数越多,内存吃的越快</p>
Spring + Hibernate4 无法将数据写入数据【请教】
<div class="iteye-blog-content-contain" style="font-size: 14px;"> <p class="MsoNormal" style="text-align: center;" align="center"> </p> <p class="MsoNormal"><span lang="EN-US"> 问题:</span></p> <p class="MsoNormal"><span lang="EN-US">通过Junit单元测试Service可以将数据写入数据库;但部署却无法向数据库写入数据。</span></p> <h1> <strong><span lang="EN-US">1、</span></strong><span lang="EN-US"><strong>环境</strong>:</span> </h1> <p class="MsoListParagraph" style="margin-left: 18.0pt; text-indent: 0cm;"><span lang="EN-US">Spring 3.1.2</span></p> <p class="MsoListParagraph" style="margin-left: 18.0pt; text-indent: 0cm;"><span lang="EN-US">Hibernate 4.1.4</span></p> <p class="MsoListParagraph" style="margin-left: 18.0pt; text-indent: 0cm;"><span lang="EN-US">Jdk1.6</span></p> <h1>2、配置:</h1> <p class="MsoListParagraph">&lt;!--[if !supportLists]--&gt;<strong><span lang="EN-US">1<span lang="EN-US">     </span></span></strong>&lt;!--[endif]--&gt;<strong><span lang="EN-US"> </span></strong></p> <p class="MsoListParagraph">&lt;!--[if !supportLists]--&gt;<strong><span lang="EN-US">2<span lang="EN-US">     </span></span></strong>&lt;!--[endif]--&gt;<strong><span lang="EN-US"> </span></strong></p> <h2> <span lang="EN-US">2.1、</span><span lang="EN-US">Web.xml</span> </h2> <p class="MsoNormal" align="left"><span lang="EN-US">&lt;?</span><span lang="EN-US">xml</span><span lang="EN-US"> <span lang="EN-US">version</span>=<em><span lang="EN-US">"1.0"</span></em> <span lang="EN-US">encoding</span>=<em><span lang="EN-US">"UTF-8"</span></em><span lang="EN-US">?&gt;</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US"> </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">&lt;</span><span lang="EN-US">web-app</span><span lang="EN-US"> <span lang="EN-US">xmlns:xsi</span>=<em><span lang="EN-US">"http://www.w3.org/2001/XMLSchema-instance"</span></em> <span lang="EN-US">xmlns</span>=<em><span lang="EN-US">"http://java.sun.com/xml/ns/javaee"</span></em> <span lang="EN-US">xmlns:web</span>=<em><span lang="EN-US">"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"</span></em> <span lang="EN-US">xsi:schemaLocation</span>=<em><span lang="EN-US">"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"</span></em> <span lang="EN-US">id</span>=<em><span lang="EN-US">"WebApp_ID"</span></em> <span lang="EN-US">version</span>=<em><span lang="EN-US">"2.5"</span></em><span lang="EN-US">&gt;</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">     </span><span lang="EN-US">&lt;</span><span lang="EN-US">display-name</span><span lang="EN-US">&gt;</span><span style="text-decoration: underline;"><span lang="EN-US">springmvc</span></span><span lang="EN-US">&lt;/</span><span lang="EN-US">display-name</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">     </span><span lang="EN-US">&lt;</span><span lang="EN-US">welcome-file-list</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">welcome-file</span><span lang="EN-US">&gt;</span><span lang="EN-US">index.html</span><span lang="EN-US">&lt;/</span><span lang="EN-US">welcome-file</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">welcome-file</span><span lang="EN-US">&gt;</span><span lang="EN-US">index.htm</span><span lang="EN-US">&lt;/</span><span lang="EN-US">welcome-file</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">welcome-file</span><span lang="EN-US">&gt;</span><span lang="EN-US">index.jsp</span><span lang="EN-US">&lt;/</span><span lang="EN-US">welcome-file</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">     </span><span lang="EN-US">&lt;/</span><span lang="EN-US">welcome-file-list</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">     </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">     </span><span lang="EN-US">&lt;!-- Spring ApplicationContext</span><span lang="EN-US">配置文件的路径</span><span lang="EN-US">,</span><span lang="EN-US">可使用通配符</span><span lang="EN-US">,</span><span lang="EN-US">多个路径用</span><span lang="EN-US">,</span><span lang="EN-US">号分隔</span><span lang="EN-US">此参数用于后面的</span><span lang="EN-US">Spring Context Loader --&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">     </span><span lang="EN-US">&lt;</span><span lang="EN-US">context-param</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><span lang="EN-US">&lt;</span><span lang="EN-US">param-name</span><span lang="EN-US">&gt;</span><span lang="EN-US">contextConfigLocation</span><span lang="EN-US">&lt;/</span><span lang="EN-US">param-name</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><span lang="EN-US">&lt;</span><span lang="EN-US">param-value</span><span lang="EN-US">&gt;</span><span style="text-decoration: underline;"><span lang="EN-US">classpath</span></span><span lang="EN-US">*:/applicationContext.xml</span><span lang="EN-US">&lt;/</span><span lang="EN-US">param-value</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">     </span><span lang="EN-US">&lt;/</span><span lang="EN-US">context-param</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">     </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">listener</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">listener-class</span><span lang="EN-US">&gt;</span><span lang="EN-US">org.springframework.web.context.ContextLoaderListener</span><span lang="EN-US">&lt;/</span><span lang="EN-US">listener-class</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;/</span><span lang="EN-US">listener</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;!--<span style="text-decoration: underline;">Dispathcer</span> <span style="text-decoration: underline;">Servlet</span> --&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">servlet</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">servlet-name</span><span lang="EN-US">&gt;</span><span lang="EN-US">spring-<span style="text-decoration: underline;">mvc</span></span><span lang="EN-US">&lt;/</span><span lang="EN-US">servlet-name</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">servlet-class</span><span lang="EN-US">&gt;</span><span lang="EN-US">org.springframework.web.servlet.DispatcherServlet</span><span lang="EN-US">&lt;/</span><span lang="EN-US">servlet-class</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">init-param</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">            </span><span lang="EN-US">&lt;</span><span lang="EN-US">param-name</span><span lang="EN-US">&gt;</span><span lang="EN-US">contextConfigLocation</span><span lang="EN-US">&lt;/</span><span lang="EN-US">param-name</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">            </span><span lang="EN-US">&lt;</span><span lang="EN-US">param-value</span><span lang="EN-US">&gt;</span><span lang="EN-US">/WEB-INF/spring-mvc.xml</span><span lang="EN-US">&lt;/</span><span lang="EN-US">param-value</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;/</span><span lang="EN-US">init-param</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">load-on-startup</span><span lang="EN-US">&gt;</span><span lang="EN-US">1</span><span lang="EN-US">&lt;/</span><span lang="EN-US">load-on-startup</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;/</span><span lang="EN-US">servlet</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;!-- Spring MVC <span style="text-decoration: underline;">Servlet</span> </span><span lang="EN-US">拦截</span><span lang="EN-US">.do</span><span lang="EN-US">结尾的请求</span><span lang="EN-US">--&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">servlet-mapping</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">servlet-name</span><span lang="EN-US">&gt;</span><span lang="EN-US">spring-<span style="text-decoration: underline;">mvc</span></span><span lang="EN-US">&lt;/</span><span lang="EN-US">servlet-name</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">url-pattern</span><span lang="EN-US">&gt;</span><span lang="EN-US">*.do</span><span lang="EN-US">&lt;/</span><span lang="EN-US">url-pattern</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;/</span><span lang="EN-US">servlet-mapping</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;!-- Filter </span><span lang="EN-US">定义</span><span lang="EN-US"> --&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">filter</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">filter-name</span><span lang="EN-US">&gt;</span><span lang="EN-US">encodingFilter</span><span lang="EN-US">&lt;/</span><span lang="EN-US">filter-name</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">filter-class</span><span lang="EN-US">&gt;</span><span lang="EN-US">org.springframework.web.filter.CharacterEncodingFilter</span><span lang="EN-US">&lt;/</span><span lang="EN-US">filter-class</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">init-param</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">            </span><span lang="EN-US">&lt;</span><span lang="EN-US">param-name</span><span lang="EN-US">&gt;</span><span lang="EN-US">encoding</span><span lang="EN-US">&lt;/</span><span lang="EN-US">param-name</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">            </span><span lang="EN-US">&lt;</span><span lang="EN-US">param-value</span><span lang="EN-US">&gt;</span><span lang="EN-US">UTF-8</span><span lang="EN-US">&lt;/</span><span lang="EN-US">param-value</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;/</span><span lang="EN-US">init-param</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">init-param</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">            </span><span lang="EN-US">&lt;</span><span lang="EN-US">param-name</span><span lang="EN-US">&gt;</span><span lang="EN-US">forceEncoding</span><span lang="EN-US">&lt;/</span><span lang="EN-US">param-name</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">            </span><span lang="EN-US">&lt;</span><span lang="EN-US">param-value</span><span lang="EN-US">&gt;</span><span lang="EN-US">true</span><span lang="EN-US">&lt;/</span><span lang="EN-US">param-value</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;/</span><span lang="EN-US">init-param</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;/</span><span lang="EN-US">filter</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">filter-mapping</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">filter-name</span><span lang="EN-US">&gt;</span><span lang="EN-US">encodingFilter</span><span lang="EN-US">&lt;/</span><span lang="EN-US">filter-name</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">url-pattern</span><span lang="EN-US">&gt;</span><span lang="EN-US">/*</span><span lang="EN-US">&lt;/</span><span lang="EN-US">url-pattern</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;/</span><span lang="EN-US">filter-mapping</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">   </span><span lang="EN-US">&lt;!--openSessionInView--&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">   </span><span lang="EN-US">&lt;</span><span lang="EN-US">filter</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><span lang="EN-US">&lt;</span><span lang="EN-US">filter-name</span><span lang="EN-US">&gt;</span><span lang="EN-US">openSessionInView</span><span lang="EN-US">&lt;/</span><span lang="EN-US">filter-name</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">filter-class</span><span lang="EN-US">&gt;</span><span lang="EN-US">org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</span><span lang="EN-US">&lt;/</span><span lang="EN-US">filter-class</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><span lang="EN-US">&lt;</span><span lang="EN-US">init-param</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">           </span><span lang="EN-US">&lt;</span><span lang="EN-US">param-name</span><span lang="EN-US">&gt;</span><span lang="EN-US">sessionFactoryBeanName</span><span lang="EN-US">&lt;/</span><span lang="EN-US">param-name</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">           </span><span lang="EN-US">&lt;</span><span lang="EN-US">param-value</span><span lang="EN-US">&gt;</span><span lang="EN-US">sessionFactory</span><span lang="EN-US">&lt;/</span><span lang="EN-US">param-value</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><span lang="EN-US">&lt;/</span><span lang="EN-US">init-param</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;/</span><span lang="EN-US">filter</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US"> </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">filter-mapping</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><span lang="EN-US">&lt;</span><span lang="EN-US">filter-name</span><span lang="EN-US">&gt;</span><span lang="EN-US">openSessionInView</span><span lang="EN-US">&lt;/</span><span lang="EN-US">filter-name</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><span lang="EN-US">&lt;</span><span lang="EN-US">url-pattern</span><span lang="EN-US">&gt;</span><span lang="EN-US">/*</span><span lang="EN-US">&lt;/</span><span lang="EN-US">url-pattern</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;/</span><span lang="EN-US">filter-mapping</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US"> </span></p> <p class="MsoListParagraph" style="margin-left: 18.0pt; text-indent: 0cm;"><span lang="EN-US">&lt;/</span><span lang="EN-US">web-app</span><span lang="EN-US">&gt;</span></p> <h2> <span lang="EN-US">2.2<span lang="EN-US">     </span></span><span lang="EN-US">applicationContext.xml</span> </h2> <p class="MsoNormal"><span lang="EN-US">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">&lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">          &lt;!--</span><span lang="EN-US">扫描并自动装配</span><span lang="EN-US"> --&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;context:annotation-config /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;context:component-scan base-package="com.sp" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         </span></p> <p class="MsoNormal"><span lang="EN-US">   &lt;bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                   &lt;property name="locations"&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                            &lt;value&gt;classpath:application.properties&lt;/value&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                   &lt;/property&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;/bean&gt;</span></p> <p class="MsoNormal"><span lang="EN-US"> </span></p> <p class="MsoNormal"><span lang="EN-US">   &lt;!-- </span><span lang="EN-US">数据源配置</span><span lang="EN-US"> --&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">    &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">        &lt;!-- Connection Info --&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">        &lt;property name="driverClassName" value="${jdbc.driver}" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">        &lt;property name="url" value="${jdbc.url}" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">        &lt;property name="username" value="${jdbc.username}" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">        &lt;property name="password" value="${jdbc.password}" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US"> </span></p> <p class="MsoNormal"><span lang="EN-US">        &lt;!-- Connection Pooling Info --&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">        &lt;property name="maxActive" value="${dbcp.maxActive}" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">        &lt;property name="maxIdle" value="${dbcp.maxIdle}" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">        &lt;property name="defaultAutoCommit" value="false" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">        </span></p> <p class="MsoNormal"><span lang="EN-US">        &lt;!-- </span><span lang="EN-US">连接</span><span lang="EN-US">Idle</span><span lang="EN-US">一个小时后超时</span><span lang="EN-US"> --&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">        &lt;property name="timeBetweenEvictionRunsMillis" value="3600000" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">        &lt;property name="minEvictableIdleTimeMillis" value="3600000" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">    &lt;/bean&gt;</span></p> <p class="MsoNormal"><span lang="EN-US"> </span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                   &lt;property name="dataSource" ref="dataSource"&gt;&lt;/property&gt;              </span></p> <p class="MsoNormal"><span lang="EN-US">                   &lt;property name="hibernateProperties"&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                            &lt;props&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                                     &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                                     &lt;prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/prop&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                                     &lt;prop key="hibernate.connection.autocommit"&gt;true&lt;/prop&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                                     &lt;prop key="hibernate.current_session_context_class"&gt;org.springframework.orm.hibernate4.SpringSessionContext&lt;/prop&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                                     &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                                     &lt;prop key="hibernate.format_sql"&gt;true&lt;/prop&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                            &lt;/props&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                   &lt;/property&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                   </span></p> <p class="MsoNormal"><span lang="EN-US">                   &lt;property name="packagesToScan"&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                            &lt;list&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                                     &lt;value&gt;com.sp.dao&lt;/value&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                                     &lt;value&gt;com.sp.entity.dict&lt;/value&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                            &lt;/list&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                   &lt;/property&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;/bean&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         </span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" abstract="false" lazy-init="default" autowire="default"&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                   &lt;property name="sessionFactory"&gt;&lt;ref bean="sessionFactory" /&gt;&lt;/property&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;/bean&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         </span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;!-- </span><span lang="EN-US">事务管理配置</span><span lang="EN-US"> --&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;!--&lt;tx:annotation-driven transaction-manager="transactionManager"&gt;&lt;/tx:annotation-driven&gt;--&gt; </span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;tx:advice id="txAdvice" transaction-manager="transactionManager"&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                   &lt;tx:attributes&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                            &lt;tx:method name="insert*" read-only="false" propagation="REQUIRED" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                            &lt;tx:method name="get*" read-only="false" propagation="REQUIRED" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">            &lt;tx:method name="add*" read-only="false" propagation="REQUIRED" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">            &lt;tx:method name="update*" read-only="false" propagation="REQUIRED" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">            &lt;tx:method name="delete*" read-only="false" propagation="REQUIRED" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                   &lt;/tx:attributes&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;/tx:advice&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         </span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;aop:config&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                   &lt;aop:advisor pointcut="execution(* com.sp.service.*.*(..))"  advice-ref="txAdvice"/&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;/aop:config&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">&lt;/beans&gt;</span></p> <p class="MsoNormal"><span lang="EN-US"> </span></p> <h2> <span lang="EN-US">2.3<span lang="EN-US"> </span></span><span lang="EN-US">spring-mvc.xml</span> </h2> <p class="MsoNormal"><span lang="EN-US">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">&lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"</span></p> <p class="MsoNormal"><span lang="EN-US">         xmlns:context="http://www.springframework.org/schema/context"</span></p> <p class="MsoNormal"><span lang="EN-US">         xmlns:mvc="http://www.springframework.org/schema/mvc"</span></p> <p class="MsoNormal"><span lang="EN-US">         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd</span></p> <p class="MsoNormal"><span lang="EN-US">                   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd</span></p> <p class="MsoNormal"><span lang="EN-US">        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"&gt;</span></p> <p class="MsoNormal"><span lang="EN-US"> </span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;!-- </span><span lang="EN-US">启动注解驱动的</span><span lang="EN-US">SpringMVC</span><span lang="EN-US">功能,注册请求</span><span lang="EN-US">URL</span><span lang="EN-US">和注解</span><span lang="EN-US">POJO</span><span lang="EN-US">类方法的映射</span><span lang="EN-US"> --&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;mvc:annotation-driven /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         </span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;!-- </span><span lang="EN-US">自动扫描且只扫描</span><span lang="EN-US">@Controller --&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;context:component-scan base-package="com.sp.web" use-default-filters="false"&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                   &lt;context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;/context:component-scan&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         </span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;context:component-scan base-package="com.sp.dao.impl"&gt;&lt;/context:component-scan&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;context:component-scan base-package="com.sp.service.impl"&gt;&lt;/context:component-scan&gt;</span></p> <p class="MsoNormal"><span lang="EN-US"> </span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;mvc:default-servlet-handler /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                   </span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;!-- </span><span lang="EN-US">对模型视图名称的解析,在请求时模型视图名称添加后缀。定义</span><span lang="EN-US">JSP --&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                   &lt;property name="prefix" value="/WEB-INF/views/" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                   &lt;property name="suffix" value=".jsp" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;/bean&gt;</span></p> <p class="MsoNormal"><span lang="EN-US"> </span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;!-- </span><span lang="EN-US">支持上传文件</span><span lang="EN-US"> --&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">&lt;/beans&gt;</span></p> <p class="MsoNormal"><span lang="EN-US"> </span></p> <h2> <span lang="EN-US">2.4<span lang="EN-US">  </span></span><span lang="EN-US">hibernate.cfg.xml</span> </h2> <p class="MsoNormal" align="left"><span lang="EN-US">&lt;?</span><span lang="EN-US">xml</span><span lang="EN-US"> <span lang="EN-US">version</span>=<em><span lang="EN-US">'1.0'</span></em> <span lang="EN-US">encoding</span>=<em><span lang="EN-US">'utf-8'</span></em><span lang="EN-US">?&gt;</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">&lt;!</span><span lang="EN-US">DOCTYPE</span><span lang="EN-US"> <span lang="EN-US">hibernate-configuration</span> <span lang="EN-US">PUBLIC</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">     <span lang="EN-US">"-//Hibernate/Hibernate Configuration DTD 3.0//EN"</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">     <span lang="EN-US">"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"</span><span lang="EN-US">&gt;</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">&lt;</span><span lang="EN-US">hibernate-configuration</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">  </span><span lang="EN-US">&lt;</span><span lang="EN-US">session-factory</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;!-- Database connection settings--&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">property</span><span lang="EN-US"> <span lang="EN-US">name</span>=<em><span lang="EN-US">"connection.driver_class"</span></em><span lang="EN-US">&gt;</span>com.mysql.jdbc.Driver<span lang="EN-US">&lt;/</span><span lang="EN-US">property</span><span lang="EN-US">&gt;</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">property</span><span lang="EN-US"> <span lang="EN-US">name</span>=<em><span lang="EN-US">"connection.url"</span></em><span lang="EN-US">&gt;</span>jdbc:mysql://<span style="text-decoration: underline;">localhost</span>:3306/test<span lang="EN-US">&lt;/</span><span lang="EN-US">property</span><span lang="EN-US">&gt;</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">property</span><span lang="EN-US"> <span lang="EN-US">name</span>=<em><span lang="EN-US">"connection.username"</span></em><span lang="EN-US">&gt;</span>root<span lang="EN-US">&lt;/</span><span lang="EN-US">property</span><span lang="EN-US">&gt;</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">property</span><span lang="EN-US"> <span lang="EN-US">name</span>=<em><span lang="EN-US">"connection.password"</span></em><span lang="EN-US">&gt;</span>password<span lang="EN-US">&lt;/</span><span lang="EN-US">property</span><span lang="EN-US">&gt;</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;!-- JDBC connection pool (use the built-in) --&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">property</span><span lang="EN-US"> <span lang="EN-US">name</span>=<em><span lang="EN-US">"connection.pool_size"</span></em><span lang="EN-US">&gt;</span>1<span lang="EN-US">&lt;/</span><span lang="EN-US">property</span><span lang="EN-US">&gt;</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;!-- SQL dialect </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    &lt;property name="dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    --&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;!-- Enable Hibernate's automatic session context management --&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;!-- &lt;property name="current_session_context_class"&gt;thread&lt;/property&gt; --&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US"> </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;!-- Disable the second-level cache  --&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;!-- &lt;property name="cache.provider_class"&gt;org.hibernate.cache.internal.NoCacheProvider&lt;/property&gt;--&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;!-- Echo all executed SQL to <span style="text-decoration: underline;">stdout</span> --&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">property</span><span lang="EN-US"> <span lang="EN-US">name</span>=<em><span lang="EN-US">"show_sql"</span></em><span lang="EN-US">&gt;</span>true<span lang="EN-US">&lt;/</span><span lang="EN-US">property</span><span lang="EN-US">&gt;</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">property</span><span lang="EN-US"> <span lang="EN-US">name</span>=<em><span lang="EN-US">"format_sql"</span></em><span lang="EN-US">&gt;</span>true<span lang="EN-US">&lt;/</span><span lang="EN-US">property</span><span lang="EN-US">&gt;</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;!-- Drop and re-create the database schema on startup--&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">property</span><span lang="EN-US"> <span lang="EN-US">name</span>=<em><span lang="EN-US">"hbm2ddl.auto"</span></em><span lang="EN-US">&gt;</span>update<span lang="EN-US">&lt;/</span><span lang="EN-US">property</span><span lang="EN-US">&gt;</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">property</span><span lang="EN-US"> <span lang="EN-US">name</span>=<em><span lang="EN-US">"javax.persistence.validation.mode"</span></em><span lang="EN-US">&gt;</span>none<span lang="EN-US">&lt;/</span><span lang="EN-US">property</span><span lang="EN-US">&gt;</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">     </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">  </span><span lang="EN-US">&lt;/</span><span lang="EN-US">session-factory</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">&lt;/</span><span lang="EN-US">hibernate-configuration</span><span lang="EN-US">&gt;</span></p> <p class="MsoListParagraph" style="margin-left: 18.0pt; text-indent: 0cm;"><strong><span lang="EN-US"> </span></strong></p> <h1> <span lang="EN-US">3<span lang="EN-US">         </span></span><span lang="EN-US">代码:</span> </h1> <p class="MsoListParagraph">&lt;!--[if !supportLists]--&gt;<strong><span lang="EN-US">1<span lang="EN-US">     </span></span></strong>&lt;!--[endif]--&gt;<strong><span lang="EN-US"> </span></strong></p> <p class="MsoListParagraph">&lt;!--[if !supportLists]--&gt;<strong><span lang="EN-US">2<span lang="EN-US">     </span></span></strong>&lt;!--[endif]--&gt;<strong><span lang="EN-US"> </span></strong></p> <p class="MsoListParagraph">&lt;!--[if !supportLists]--&gt;<strong><span lang="EN-US">3<span lang="EN-US">     </span></span></strong>&lt;!--[endif]--&gt;<strong><span lang="EN-US"> </span></strong></p> <h2> <span lang="EN-US">3.1<span lang="EN-US">  </span></span><span lang="EN-US">Service</span><span lang="EN-US">层:</span> </h2> <p class="MsoNormal" align="left"><span lang="EN-US">@Component</span><span lang="EN-US">(</span><span lang="EN-US">"userService"</span><span lang="EN-US">)</span></p> <p class="MsoNormal" align="left"><strong><span lang="EN-US">public</span></strong><strong><span lang="EN-US">class</span></strong><span lang="EN-US"> UserServiceImpl </span><strong><span lang="EN-US">implements</span></strong><span lang="EN-US"> UserService{</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">@<span lang="EN-US">Autowired</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><strong><span lang="EN-US">private</span></strong><span lang="EN-US"> UserDao </span><span lang="EN-US">userDao</span><span lang="EN-US">;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US"> </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">@<span lang="EN-US">Autowired</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><strong><span lang="EN-US">private</span></strong><span lang="EN-US"> FormValidator </span><span lang="EN-US">validator</span><span lang="EN-US">;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">@Override</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><strong><span lang="EN-US">public</span></strong><span lang="EN-US"> String userLogin(UserModel user) {</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       String password = </span><span lang="EN-US">userDao</span><span lang="EN-US">.userLogin(user);</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><strong><span lang="EN-US">if</span></strong><span lang="EN-US">(password.equals(user.getPassword())){</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">           </span><strong><span lang="EN-US">return</span></strong><span lang="EN-US">"sucess"</span><span lang="EN-US">;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       }</span><strong><span lang="EN-US">else</span></strong><span lang="EN-US">{</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">           </span><strong><span lang="EN-US">return</span></strong><span lang="EN-US">"false"</span><span lang="EN-US">;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       }</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    }</span></p> <p class="MsoNormal" align="left"><span lang="EN-US"> </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">@Override</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><strong><span lang="EN-US">public</span></strong><span lang="EN-US"> List&lt;ProjectType&gt; getAllProjectTypes(){</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       List&lt;ProjectType&gt; projectType = </span><span lang="EN-US">userDao</span><span lang="EN-US">.getAllProjectTypes();</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><strong><span lang="EN-US">return</span></strong><span lang="EN-US"> projectType;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    }</span></p> <p class="MsoNormal" align="left"><span lang="EN-US"> </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">//</span><span lang="EN-US">添加《项目类型》</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">@Override</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><strong><span lang="EN-US">public</span></strong><span lang="EN-US"> String addProjectType(</span><span lang="EN-US">@ModelAttribute</span><span lang="EN-US">(</span><span lang="EN-US">"newProjectType"</span><span lang="EN-US">)ProjectType projectType,BindingResult result,SessionStatus status){</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><span lang="EN-US">validator</span><span lang="EN-US">.validate(projectType, result);</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><strong><span lang="EN-US">if</span></strong><span lang="EN-US">(result.hasErrors()){</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">           </span><strong><span lang="EN-US">return</span></strong><span lang="EN-US">"newProjectType"</span><span lang="EN-US">;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       }</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><span lang="EN-US">userDao</span><span lang="EN-US">.save(projectType);</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><span lang="EN-US">//status.setComplete();</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><strong><span lang="EN-US">return</span></strong><span lang="EN-US">"redirect:showProjectTypes.do"</span><span lang="EN-US">;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    }</span></p> <p class="MsoNormal" align="left"><span lang="EN-US"> </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">//</span><span lang="EN-US">添加《项目类型》</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">@Override</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><strong><span lang="EN-US">public</span></strong><span lang="EN-US"> String addProjectType(ProjectType projectType) {</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><span lang="EN-US">userDao</span><span lang="EN-US">.save(projectType);</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><strong><span lang="EN-US">return</span></strong><span lang="EN-US">"redirect:showProjectTypes.do"</span><span lang="EN-US">;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    }</span></p> <p class="MsoNormal"><span lang="EN-US">}</span></p> <h2> <span lang="EN-US">3.2<span lang="EN-US">   </span></span><span lang="EN-US">Control</span><span lang="EN-US">层</span> </h2> <p class="MsoNormal"><span lang="EN-US">@Controller</span></p> <p class="MsoNormal"><span lang="EN-US">@RequestMapping(value = "/user")</span></p> <p class="MsoNormal"><span lang="EN-US">public class UserController {</span></p> <p class="MsoNormal"><span lang="EN-US"> </span></p> <p class="MsoNormal"><span lang="EN-US">         @Autowired</span></p> <p class="MsoNormal"><span lang="EN-US">         private UserService userService;</span></p> <p class="MsoNormal"><span lang="EN-US">         </span></p> <p class="MsoNormal"><span lang="EN-US">    @Autowired</span></p> <p class="MsoNormal"><span lang="EN-US">    private FormValidator validator;</span></p> <p class="MsoNormal"><span lang="EN-US">    </span></p> <p class="MsoNormal"><span lang="EN-US">         @RequestMapping(value = "/login",method=RequestMethod.POST)</span></p> <p class="MsoNormal"><span lang="EN-US">         protected String handle(UserModel user,BindingResult result, Model model){</span></p> <p class="MsoNormal"><span lang="EN-US">                   String flag = userService.userLogin(user);</span></p> <p class="MsoNormal"><span lang="EN-US">                   System.out.println("flag:" + flag);</span></p> <p class="MsoNormal"><span lang="EN-US">                   if ("sucess".equals(flag)){</span></p> <p class="MsoNormal"><span lang="EN-US">                            return "sucess";</span></p> <p class="MsoNormal"><span lang="EN-US">                   }</span></p> <p class="MsoNormal"><span lang="EN-US">                   return "test1";</span></p> <p class="MsoNormal"><span lang="EN-US">         }</span></p> <p class="MsoNormal"><span lang="EN-US">         </span></p> <p class="MsoNormal"><span lang="EN-US">         @RequestMapping(value = "/showProjectTypes")</span></p> <p class="MsoNormal"><span lang="EN-US">         protected ModelAndView getAllProjectTypes(){</span></p> <p class="MsoNormal"><span lang="EN-US">                   ModelAndView mav = new ModelAndView("showProjectTypes");</span></p> <p class="MsoNormal"><span lang="EN-US">                   List&lt;ProjectType&gt; projectTypes = userService.getAllProjectTypes();</span></p> <p class="MsoNormal"><span lang="EN-US">                   mav.addObject("SEARCH_PROJECTTYPE_RESULTS_KEY", projectTypes);</span></p> <p class="MsoNormal"><span lang="EN-US">                   return mav;</span></p> <p class="MsoNormal"><span lang="EN-US">         }</span></p> <p class="MsoNormal"><span lang="EN-US">         </span></p> <p class="MsoNormal"><span lang="EN-US">         @RequestMapping(value = "/addProjectType",method=RequestMethod.GET)</span></p> <p class="MsoNormal"><span lang="EN-US">         protected ModelAndView newProjectTypeForm(){</span></p> <p class="MsoNormal"><span lang="EN-US">                   ModelAndView mav = new ModelAndView("newProjectType");</span></p> <p class="MsoNormal"><span lang="EN-US">                   ProjectType projectType = new ProjectType();</span></p> <p class="MsoNormal"><span lang="EN-US">                   mav.getModelMap().put("newProjectType", projectType);</span></p> <p class="MsoNormal"><span lang="EN-US">                   return mav;</span></p> <p class="MsoNormal"><span lang="EN-US">         }</span></p> <p class="MsoNormal"><span lang="EN-US">         </span></p> <p class="MsoNormal"><span lang="EN-US">         @RequestMapping(value = "/saveProjectType",method=RequestMethod.POST)</span></p> <p class="MsoNormal"><span lang="EN-US">         protected String addProjectType(@ModelAttribute("newProjectType")ProjectType projectType,BindingResult result,SessionStatus status){</span></p> <p class="MsoNormal"><span lang="EN-US">                   //return userService.addProjectType(projectType, result, status);</span></p> <p class="MsoNormal"><span lang="EN-US">                   return userService.addProjectType(projectType);</span></p> <p class="MsoNormal"><span lang="EN-US">         }</span></p> <p class="MsoNormal"><span lang="EN-US">}</span></p> <p class="MsoNormal"><span lang="EN-US"> </span></p> <h2> <span lang="EN-US">3.3<span lang="EN-US">    </span></span><span lang="EN-US">DAO</span><span lang="EN-US">@Component("userDao")</span> </h2> <p class="MsoNormal"><span lang="EN-US">public class UserDaoImpl implements UserDao{</span></p> <p class="MsoNormal"><span lang="EN-US">         @Autowired</span></p> <p class="MsoNormal"><span lang="EN-US">         private SessionFactory sessionFactory;</span></p> <p class="MsoNormal"><span lang="EN-US">    </span></p> <p class="MsoNormal"><span lang="EN-US">    public ProjectType getById(int id)</span></p> <p class="MsoNormal"><span lang="EN-US">    {</span></p> <p class="MsoNormal"><span lang="EN-US">        return (ProjectType) sessionFactory.getCurrentSession().get(ProjectType.class, id);</span></p> <p class="MsoNormal"><span lang="EN-US">    }</span></p> <p class="MsoNormal"><span lang="EN-US">    </span></p> <p class="MsoNormal"><span lang="EN-US">         @Override</span></p> <p class="MsoNormal"><span lang="EN-US">         public String userLogin(UserModel user) {</span></p> <p class="MsoNormal"><span lang="EN-US">                   return "123456";</span></p> <p class="MsoNormal"><span lang="EN-US">         }</span></p> <p class="MsoNormal"><span lang="EN-US"> </span></p> <p class="MsoNormal"><span lang="EN-US">         //</span><span lang="EN-US">获取数据</span></p> <p class="MsoNormal"><span lang="EN-US">         @Override</span></p> <p class="MsoNormal"><span lang="EN-US">         @SuppressWarnings("unchecked")</span></p> <p class="MsoNormal"><span lang="EN-US">         public List&lt;ProjectType&gt; getAllProjectTypes() {</span></p> <p class="MsoNormal"><span lang="EN-US">                   Criteria ceriteria = sessionFactory.getCurrentSession().createCriteria(ProjectType.class);</span></p> <p class="MsoNormal"><span lang="EN-US">                   return ceriteria.list();</span></p> <p class="MsoNormal"><span lang="EN-US">         }</span></p> <p class="MsoNormal"><span lang="EN-US"> </span></p> <p class="MsoNormal"><span lang="EN-US">         //</span><span lang="EN-US">新增数据;</span></p> <p class="MsoNormal"><span lang="EN-US">         @Override</span></p> <p class="MsoNormal"><span lang="EN-US">//      @Transactional(readOnly = true, propagation = Propagation.REQUIRED)</span></p> <p class="MsoNormal"><span lang="EN-US">         public int save(ProjectType projectType) {</span></p> <p class="MsoNormal"><span lang="EN-US">                   System.out.println("save:---");</span></p> <p class="MsoNormal"><span lang="EN-US">                   System.out.println(projectType.getIndex());</span></p> <p class="MsoNormal"><span lang="EN-US">                   System.out.println(projectType.getProjectType());</span></p> <p class="MsoNormal"><span lang="EN-US">                   System.out.println(projectType.getProjectTypeAbbr());</span></p> <p class="MsoNormal"><span lang="EN-US">                   </span></p> <p class="MsoNormal"><span lang="EN-US">                   return (Integer) sessionFactory.getCurrentSession().save(projectType);</span></p> <p class="MsoNormal"><span lang="EN-US">         }</span></p> <p> </p> <p class="MsoNormal"><span lang="EN-US">}</span></p> </div>
JavaCV调用摄像头如何调节分辨率?高分 求高手
求高手! 想用JavaCV获取摄像头图像并拍照,买的高分辨率的摄像头,可是拍出来的照片才55kb,远不及直接连电脑拍出来的效果,请教如何在java程序中调节分辨率 http://blog.csdn.net/sivyer123/article/details/22411861 这是示例程序 public class video { public static void main(String[] args) throws Exception { final String savedImageFile = "d:\\my.jpg"; //拍摄所存储的文件 OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0); grabber.start(); //开始获取摄像头数据 IplImage image =grabber.grab(); //将所获取摄像头数据放入IplImage int width = image.width(); int height = image.height(); final BufferedImage bImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D bGraphics = bImage.createGraphics(); CanvasFrame canvas = new CanvasFrame("Camera",1); canvas.setCanvasSize(width, height); canvas.getCanvas().addMouseListener(new MouseAdapter(){ public void mouseClicked(MouseEvent e){ try{ ImageIO.write(bImage, "jpg", new File(savedImageFile));//数据写入my.jpg } catch (IOException e1){ // TODO Auto-generated catch block e1.printStackTrace(); } } }); while(canvas.isVisible() && (image=grabber.grab()) != null){ canvas.showImage(image); bGraphics.drawImage(image.getBufferedImage(),null,0,0); } cvReleaseImage(image); grabber.stop(); canvas.dispose(); } }
关于java调用cmd执行批处理和获取控制台输出的问题
``` package cn.zjcscut.compile.cmd; import cn.zjcscut.compile.cmd.util.ClassUtils; import cn.zjcscut.compile.cmd.util.StreamGobbler; import cn.zjcscut.freemarker.classUtils.ClassLoader; import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.io.*; /** * @author zjc * @version 2016/7/15 23:09 * @function */ public class DynamicCompileWithCmd { private static final Logger log = LogManager.getLogger(DynamicCompileWithCmd.class); private static final String SAVEPATH = ClassLoader.getUserDir() + "/bin/source/"; private static final String CLASSSUFFIX = ".java"; private static final String BATSUFFIX = ".bat"; private String className; private String result; public void save(String content) { className = ClassUtils.getClassNameFromSourceCode(content); if (StringUtils.isEmpty(className)) { log.error("获取类名失败,估计是类的结构出现异常"); throw new RuntimeException("class format error,please check it!"); } String realFile = SAVEPATH + className + CLASSSUFFIX; File file = new File(realFile); if (!file.exists()) { file.getParentFile().mkdirs(); //不存在就创建 } BufferedWriter bf = null; FileWriter fw = null; try { fw = new FileWriter(file); bf = new BufferedWriter(fw); bf.write(content); bf.close(); fw.close(); } catch (Exception e) { log.error("写入源代码生成.java文件失败"); throw new RuntimeException("create java file failed"); } finally { if (bf != null) { try { bf.close(); } catch (IOException e) { e.printStackTrace(); } } if (fw != null) { try { fw.close(); } catch (IOException e) { e.printStackTrace(); } } } } private String createBatFile(String className) { String realFile = SAVEPATH + className + BATSUFFIX; File file = new File(realFile); if (!file.exists()) { file.getParentFile().mkdirs(); //不存在就创建 } String compile = "javac " + className + CLASSSUFFIX + " \n"; String cd = "cd " + SAVEPATH + " \n"; String run = "java " + className + " \n"; BufferedWriter bf = null; FileWriter fw = null; try { fw = new FileWriter(file); bf = new BufferedWriter(fw); bf.write(cd + compile + run); bf.close(); fw.close(); return realFile; } catch (Exception e) { log.error("写入bat文件失败"); throw new RuntimeException("create bat file failed"); } finally { if (bf != null) { try { bf.close(); } catch (IOException e) { e.printStackTrace(); } } if (fw != null) { try { fw.close(); } catch (IOException e) { e.printStackTrace(); } } } } private static Process process;//执行子进程 public String execute(String content) { save(content); String batName = createBatFile(className); Runtime runtime = Runtime.getRuntime(); BufferedWriter bw = null; try { // Thread.sleep(1000); //防止cmd.exe程序未启动,当然也可以直接使用javac命令 // bw = new BufferedWriter(new OutputStreamWriter(process.getOutputStream())); // String compile = "javac " + SAVEPATH + className + CLASSSUFFIX + " \n"; // String run ="java " + SAVEPATH + className + " \n"; long startFreeMemory = runtime.freeMemory(); //记录开始执行命令前的空闲内存 long startCurrentTime = System.currentTimeMillis(); // bw.write("java " + SAVEPATH + className + " \n"); // bw.write(compile + " & " + run); // bw.flush(); // bw.close(); // "cmd.exe /C start /b " process = runtime.exec("cmd.exe /C start /b " + batName); //合并流 SequenceInputStream sis = new SequenceInputStream(process.getInputStream(),process.getErrorStream()); InputStreamReader isr = new InputStreamReader (sis, "GBK"); BufferedReader br = new BufferedReader(isr); String line = null; while (null != ( line = br.readLine () )) { System.out.println (line); } process.destroy (); br.close (); isr.close (); process.waitFor(); int sign = process.exitValue(); if (sign == 0) { System.out.println("执行成功"); } else { System.out.println("执行失败"); } // StreamGobbler outGobbler = new StreamGobbler(process.getInputStream(), "STDOUT"); // Thread thread1 = new Thread(outGobbler); // thread1.start(); // new Thread(new Runnable() { // @Override // public void run() { // try { // // String s = null; // BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream())); // while ((s = br.readLine()) != null) { // System.out.println(s); // result += s; // } // } catch (java.lang.Exception e) { // e.printStackTrace(); // } // } // }).start(); // // new Thread(new Runnable() { // @Override // public void run() { // try { // // String s = null; // BufferedReader br = new BufferedReader(new InputStreamReader(process.getErrorStream())); // while ((s = br.readLine()) != null) { // System.out.println("error -- " + s); // } // } catch (java.lang.Exception e) { // e.printStackTrace(); // } // } // }).start(); // StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), "ERROR"); // Thread thread = new Thread(errorGobbler); // thread.start(); // Thread thread = new Thread(new RuntimeResultThread()); // thread.start(); // String s = null; // BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream())); // while ((s = br.readLine()) != null) { // System.out.println(s); // result += s; // } // // br.close(); // System.out.println(batName); // InputStream in = process.getInputStream(); // // int c; // while ((c = in.read()) != -1) { // System.out.print("输出---" + c);//如果你不需要看输出,这行可以注销掉 // } // in.close(); //获取控制台输出结果 // Thread thread = new Thread(new RuntimeResultThread()); // thread.start(); long endCurrentTime = System.currentTimeMillis(); long endFreeMemory = runtime.freeMemory(); double useMemory = (startFreeMemory - endFreeMemory) / 1024; long useTime = endCurrentTime - startCurrentTime; result += "\n\ncost time : " + useTime + " ms" + ", cost memory : " + useMemory + " mb"; return result; } catch (Exception e) { e.printStackTrace(); } finally { if (bw != null) { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } private class RuntimeResultThread implements Runnable { @Override public void run() { BufferedReader br = null; String content = null; try { br = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK")); while ((content = br.readLine()) != null) { System.out.println("content" + content); result += content; } br.close(); } catch (Exception e) { log.error("获取控制台输出失败"); e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } } } } ``` 上边的代码是为了实现动态编译和执行输入的java源码,原理是先把.java和bat文件输出到工作目录下的一个文件夹,调用cmd编译.java文件,再执行.class。现在出现一个问题,就是获取控制台输出的时候总是阻塞,google这个问题说是process的getErrorStream缓冲没有清空,导致一直阻塞,需要使用多线程操作,但是用了多线程发现,process的getInputStream部分信息丢失了。 ![图片说明](https://img-ask.csdn.net/upload/201607/16/1468647055_939794.png) 想请教有什么解决方法 ?
图片加水印时文本为unicode编码乱码问题
请教一个问题。 我在把文字写入图片时,如果字符串为unicode编码的话,生成的图片部分字符乱码,不过这个字符串如果使用log4j编码为utf-8打印到日志文件时是正常的,但在console控制台打印出来跟图片是一样的,也是部分字符乱码那。 但在debug时看到的字符串是正常的,写到图片里就乱码了。 代码如下: [code="java"] /** * <pre> * 文字水印。 * 超过屏幕可视区宽度时自动换行; * </pre> * * @param pressTexts 水印文字,字符串数组 * @param targetImg 目标图片路径 * @param outputImg 输出图片路径 * @param font 包含字体名称、字体样式、字体大小的Font对象 * @param color 字体颜色 * @param x 修正值 * @param widthMargin 宽度两边间隔 * @param y 修正值 * @param heightMargin 高度上下间隔 * @param alpha 透明度 */ public static void pressText2MultiImgs(String[] pressTexts, String targetImg, String outputImg, Font font, Color color, int x, int widthMargin, int y, int heightMargin, float alpha) { try { File img = new File(targetImg); Image src = ImageIO.read(img); int width = src.getWidth(null); int height = src.getHeight(null); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics(); g.drawImage(src, 0, 0, width, height, null); g.setColor(color); g.setFont(font); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); int txtZoneWidth = width - widthMargin;//文字区域宽度,两边留间隙 int txtZoneHeight = height - heightMargin;//文字区域高度,上下留间隙 int lineNum = 0;//当前行数 // 处理多行字符 int fontSize = font.getSize(); for(String pressText : pressTexts){ List<String> strLines = getLineStr(pressText, fontSize, txtZoneWidth); for(String str : strLines){ log.debug("行:" + str);//1 lineNum++; g.drawString(str, x + (widthMargin / 2), fontSize * lineNum + (heightMargin / 2) + y); } } g.dispose(); File imgOutput = new File(outputImg); ImageIO.write((BufferedImage) image, "jpg", imgOutput); } catch (Exception e) { e.printStackTrace(); } } [/code] 测试代码如下: [code="java"] @Test public void pressText2MultiImgsTest2(){ String pinyin = "āáăàaēéĕèeīíĭìiōóŏòoūúŭùuǖǘǚǜü"; log.info(pinyin); ImageUtils.pressText2MultiImgs(new String[]{pinyin}, "D:/temp/createImg/template.jpg", "D:/temp/createImg/out/out_3.jpg", new Font("宋体", 36, 16), Color.black, 8, 32, 8, 20, 1.0f); } [/code] 这一串字符串中 "āáăàaēéĕèeīíĭìiōóŏòoūúŭùuǖǘǚǜü"有几个是写不到图片上的,是否有解决办法 呢 :(
java用dom更新xml的问题,怎么在子节点下添加节点?
<p><span style="color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 24px;">有原始xml如下:</span></p> <p> </p> <pre name="code" class="xml">&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt; &lt;students&gt; &lt;student&gt; &lt;name sn="03" sn2="0322"/&gt; &lt;/student&gt; &lt;/students&gt;</pre> <p> <span style="color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 24px;">我想要得到修改后的结果为:</span></p> <p> </p> <pre name="code" class="xml">&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt; &lt;students&gt; &lt;student&gt; &lt;name sn="03" sn2="0322"/&gt; &lt;name sn="04" sn2="0422"/&gt; &lt;/student&gt; &lt;/students&gt;</pre> <p> <span style="color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 24px;">我的代码为:</span></p> <p> </p> <pre name="code" class="java">public static void main(String[] args) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse("D:/students.xml"); Element eltName = doc.createElement("name"); Attr attr = doc.createAttribute("sn"); attr.setValue("04"); Attr attr2 = doc.createAttribute("sn2"); attr2.setValue("0422"); eltName.setAttributeNode(attr); eltName.setAttributeNode(attr2); Element eltRoot=doc.getDocumentElement(); eltRoot.appendChild(eltName); doc2XmlFile(doc, "D:/students.xml"); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 将Document对象修改后写入到xml里面 * @param document Document对象 * @param filename xml文件路径 * @return */ public boolean doc2XmlFile(Document document, String filename) { boolean flag = true; try { /** 将document中的内容写入文件中 */ TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); /** 编码 */ transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(new File(filename)); transformer.transform(source, result); } catch (Exception ex) { flag = false; System.out.println("更新" + filename + "出错:" + ex); log.error("更新" + filename + "出错:" + ex); ex.printStackTrace(); } return flag; }</pre> <p> <span style="color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 24px;">这样得到的结果为:</span></p> <p> </p> <pre name="code" class="xml">&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt; &lt;students&gt; &lt;student&gt; &lt;name sn="03" sn2="0322"/&gt; &lt;/student&gt; &lt;name sn="04" sn2="0422"/&gt; &lt;/students&gt;</pre> <p> <span style="color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 24px;">弄了一天了也没能把添加的name节点放到student节点下面,请教各位大侠了!</span></p> <p> </p> <p> </p> <p> </p> <p> </p>
Java学习的正确打开方式
在博主认为,对于入门级学习java的最佳学习方法莫过于视频+博客+书籍+总结,前三者博主将淋漓尽致地挥毫于这篇博客文章中,至于总结在于个人,实际上越到后面你会发现学习的最好方式就是阅读参考官方文档其次就是国内的书籍,博客次之,这又是一个层次了,这里暂时不提后面再谈。博主将为各位入门java保驾护航,各位只管冲鸭!!!上天是公平的,只要不辜负时间,时间自然不会辜负你。 何谓学习?博主所理解的学习,它是一个过程,是一个不断累积、不断沉淀、不断总结、善于传达自己的个人见解以及乐于分享的过程。
程序员必须掌握的核心算法有哪些?
由于我之前一直强调数据结构以及算法学习的重要性,所以就有一些读者经常问我,数据结构与算法应该要学习到哪个程度呢?,说实话,这个问题我不知道要怎么回答你,主要取决于你想学习到哪些程度,不过针对这个问题,我稍微总结一下我学过的算法知识点,以及我觉得值得学习的算法。这些算法与数据结构的学习大多数是零散的,并没有一本把他们全部覆盖的书籍。下面是我觉得值得学习的一些算法以及数据结构,当然,我也会整理一些看过...
前端 | 2. 正则
转载请注明以下: 本文转自清自以敬的博客:https://blog.csdn.net/qq_45791147 文章目录1.转义2.正则表达式初步2.1.匹配字符2.1.1.组成元素2.1.2.基础正则的设计 1.转义 转义的作用: 当某个字符在表达式中具有特殊含义,例如字符串引号中出现了引号,为了可以使用这些字符本身,而不是使用其在表达式中的特殊含义,则需要通过转义符“\”来构建该字符转义...
有哪些让程序员受益终生的建议
从业五年多,辗转两个大厂,出过书,创过业,从技术小白成长为基层管理,联合几个业内大牛回答下这个问题,希望能帮到大家,记得帮我点赞哦。 敲黑板!!!读了这篇文章,你将知道如何才能进大厂,如何实现财务自由,如何在工作中游刃有余,这篇文章很长,但绝对是精品,记得帮我点赞哦!!!! 一腔肺腑之言,能看进去多少,就看你自己了!!! 目录: 在校生篇: 为什么要尽量进大厂? 如何选择语言及方...
大学四年自学走来,这些私藏的实用工具/学习网站我贡献出来了
大学四年,看课本是不可能一直看课本的了,对于学习,特别是自学,善于搜索网上的一些资源来辅助,还是非常有必要的,下面我就把这几年私藏的各种资源,网站贡献出来给你们。主要有:电子书搜索、实用工具、在线视频学习网站、非视频学习网站、软件下载、面试/求职必备网站。 注意:文中提到的所有资源,文末我都给你整理好了,你们只管拿去,如果觉得不错,转发、分享就是最大的支持了。 一、电子书搜索 对于大部分程序员...
linux系列之常用运维命令整理笔录
本博客记录工作中需要的linux运维命令,大学时候开始接触linux,会一些基本操作,可是都没有整理起来,加上是做开发,不做运维,有些命令忘记了,所以现在整理成博客,当然vi,文件操作等就不介绍了,慢慢积累一些其它拓展的命令,博客不定时更新 free -m 其中:m表示兆,也可以用g,注意都要小写 Men:表示物理内存统计 total:表示物理内存总数(total=used+free) use...
比特币原理详解
一、什么是比特币 比特币是一种电子货币,是一种基于密码学的货币,在2008年11月1日由中本聪发表比特币白皮书,文中提出了一种去中心化的电子记账系统,我们平时的电子现金是银行来记账,因为银行的背后是国家信用。去中心化电子记账系统是参与者共同记账。比特币可以防止主权危机、信用风险。其好处不多做赘述,这一层面介绍的文章很多,本文主要从更深层的技术原理角度进行介绍。 二、问题引入 假设现有4个人...
程序员接私活怎样防止做完了不给钱?
首先跟大家说明一点,我们做 IT 类的外包开发,是非标品开发,所以很有可能在开发过程中会有这样那样的需求修改,而这种需求修改很容易造成扯皮,进而影响到费用支付,甚至出现做完了项目收不到钱的情况。 那么,怎么保证自己的薪酬安全呢? 我们在开工前,一定要做好一些证据方面的准备(也就是“讨薪”的理论依据),这其中最重要的就是需求文档和验收标准。一定要让需求方提供这两个文档资料作为开发的基础。之后开发...
网页实现一个简单的音乐播放器(大佬别看。(⊙﹏⊙))
今天闲着无事,就想写点东西。然后听了下歌,就打算写个播放器。 于是乎用h5 audio的加上js简单的播放器完工了。 演示地点演示 html代码如下` music 这个年纪 七月的风 音乐 ` 然后就是css`*{ margin: 0; padding: 0; text-decoration: none; list-...
Python十大装B语法
Python 是一种代表简单思想的语言,其语法相对简单,很容易上手。不过,如果就此小视 Python 语法的精妙和深邃,那就大错特错了。本文精心筛选了最能展现 Python 语法之精妙的十个知识点,并附上详细的实例代码。如能在实战中融会贯通、灵活使用,必将使代码更为精炼、高效,同时也会极大提升代码B格,使之看上去更老练,读起来更优雅。
数据库优化 - SQL优化
以实际SQL入手,带你一步一步走上SQL优化之路!
2019年11月中国大陆编程语言排行榜
2019年11月2日,我统计了某招聘网站,获得有效程序员招聘数据9万条。针对招聘信息,提取编程语言关键字,并统计如下: 编程语言比例 rank pl_ percentage 1 java 33.62% 2 cpp 16.42% 3 c_sharp 12.82% 4 javascript 12.31% 5 python 7.93% 6 go 7.25% 7 p...
通俗易懂地给女朋友讲:线程池的内部原理
餐盘在灯光的照耀下格外晶莹洁白,女朋友拿起红酒杯轻轻地抿了一小口,对我说:“经常听你说线程池,到底线程池到底是个什么原理?”
《奇巧淫技》系列-python!!每天早上八点自动发送天气预报邮件到QQ邮箱
将代码部署服务器,每日早上定时获取到天气数据,并发送到邮箱。 也可以说是一个小型人工智障。 知识可以运用在不同地方,不一定非是天气预报。
经典算法(5)杨辉三角
杨辉三角 是经典算法,这篇博客对它的算法思想进行了讲解,并有完整的代码实现。
腾讯算法面试题:64匹马8个跑道需要多少轮才能选出最快的四匹?
昨天,有网友私信我,说去阿里面试,彻底的被打击到了。问了为什么网上大量使用ThreadLocal的源码都会加上private static?他被难住了,因为他从来都没有考虑过这个问题。无独有偶,今天笔者又发现有网友吐槽了一道腾讯的面试题,我们一起来看看。 腾讯算法面试题:64匹马8个跑道需要多少轮才能选出最快的四匹? 在互联网职场论坛,一名程序员发帖求助到。二面腾讯,其中一个算法题:64匹...
面试官:你连RESTful都不知道我怎么敢要你?
干货,2019 RESTful最贱实践
Docker 从入门到掉坑
Docker 介绍 简单的对docker进行介绍,可以把它理解为一个应用程序执行的容器。但是docker本身和虚拟机还是有较为明显的出入的。我大致归纳了一下,可以总结为以下几点: docker自身也有着很多的优点,关于它的优点,可以总结为以下几项: 安装docker 从 2017 年 3 月开始 docker 在原来的基础上分为两个分支版本: Docker CE 和 Doc...
为啥国人偏爱Mybatis,而老外喜欢Hibernate/JPA呢?
关于SQL和ORM的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行了一番讨论,感触还是有一些,于是就有了今天这篇文。 声明:本文不会下关于Mybatis和JPA两个持久层框架哪个更好这样的结论。只是摆事实,讲道理,所以,请各位看官勿喷。 一、事件起因 关于Mybatis和JPA孰优孰劣的问题,争论已经很多年了。一直也没有结论,毕竟每个人的喜好和习惯是大不相同的。我也看...
白话阿里巴巴Java开发手册高级篇
不久前,阿里巴巴发布了《阿里巴巴Java开发手册》,总结了阿里巴巴内部实际项目开发过程中开发人员应该遵守的研发流程规范,这些流程规范在一定程度上能够保证最终的项目交付质量,通过在时间中总结模式,并推广给广大开发人员,来避免研发人员在实践中容易犯的错误,确保最终在大规模协作的项目中达成既定目标。 无独有偶,笔者去年在公司里负责升级和制定研发流程、设计模板、设计标准、代码标准等规范,并在实际工作中进行...
SQL-小白最佳入门sql查询一
不要偷偷的查询我的个人资料,即使你再喜欢我,也不要这样,真的不好;
项目中的if else太多了,该怎么重构?
介绍 最近跟着公司的大佬开发了一款IM系统,类似QQ和微信哈,就是聊天软件。我们有一部分业务逻辑是这样的 if (msgType = "文本") { // dosomething } else if(msgType = "图片") { // doshomething } else if(msgType = "视频") { // doshomething } else { // doshom...
Nginx 原理和架构
Nginx 是一个免费的,开源的,高性能的 HTTP 服务器和反向代理,以及 IMAP / POP3 代理服务器。Nginx 以其高性能,稳定性,丰富的功能,简单的配置和低资源消耗而闻名。 Nginx 的整体架构 Nginx 里有一个 master 进程和多个 worker 进程。master 进程并不处理网络请求,主要负责调度工作进程:加载配置、启动工作进程及非停升级。worker 进程负责处...
Python 编程开发 实用经验和技巧
Python是一门很灵活的语言,也有很多实用的方法,有时候实现一个功能可以用多种方法实现,我这里总结了一些常用的方法和技巧,包括小数保留指定位小数、判断变量的数据类型、类方法@classmethod、制表符中文对齐、遍历字典、datetime.timedelta的使用等,会持续更新......
YouTube排名第一的励志英文演讲《Dream(梦想)》
Idon’t know what that dream is that you have, I don't care how disappointing it might have been as you've been working toward that dream,but that dream that you’re holding in your mind, that it’s po...
“狗屁不通文章生成器”登顶GitHub热榜,分分钟写出万字形式主义大作
一、垃圾文字生成器介绍 最近在浏览GitHub的时候,发现了这样一个骨骼清奇的雷人项目,而且热度还特别高。 项目中文名:狗屁不通文章生成器 项目英文名:BullshitGenerator 根据作者的介绍,他是偶尔需要一些中文文字用于GUI开发时测试文本渲染,因此开发了这个废话生成器。但由于生成的废话实在是太过富于哲理,所以最近已经被小伙伴们给玩坏了。 他的文风可能是这样的: 你发现,...
程序员:我终于知道post和get的区别
是一个老生常谈的话题,然而随着不断的学习,对于以前的认识有很多误区,所以还是需要不断地总结的,学而时习之,不亦说乎
《程序人生》系列-这个程序员只用了20行代码就拿了冠军
你知道的越多,你不知道的越多 点赞再看,养成习惯GitHub上已经开源https://github.com/JavaFamily,有一线大厂面试点脑图,欢迎Star和完善 前言 这一期不算《吊打面试官》系列的,所有没前言我直接开始。 絮叨 本来应该是没有这期的,看过我上期的小伙伴应该是知道的嘛,双十一比较忙嘛,要值班又要去帮忙拍摄年会的视频素材,还得搞个程序员一天的Vlog,还要写BU...
程序员把地府后台管理系统做出来了,还有3.0版本!12月7号最新消息:已在开发中有github地址
第一幕:缘起 听说阎王爷要做个生死簿后台管理系统,我们派去了一个程序员…… 996程序员做的梦: 第一场:团队招募 为了应对地府管理危机,阎王打算找“人”开发一套地府后台管理系统,于是就在地府总经办群中发了项目需求。 话说还是中国电信的信号好,地府都是满格,哈哈!!! 经常会有外行朋友问:看某网站做的不错,功能也简单,你帮忙做一下? 而这次,面对这样的需求,这个程序员...
网易云6亿用户音乐推荐算法
网易云音乐是音乐爱好者的集聚地,云音乐推荐系统致力于通过 AI 算法的落地,实现用户千人千面的个性化推荐,为用户带来不一样的听歌体验。 本次分享重点介绍 AI 算法在音乐推荐中的应用实践,以及在算法落地过程中遇到的挑战和解决方案。 将从如下两个部分展开: AI算法在音乐推荐中的应用 音乐场景下的 AI 思考 从 2013 年 4 月正式上线至今,网易云音乐平台持续提供着:乐屏社区、UGC...
为什么要学数据结构?
一、前言 在可视化化程序设计的今天,借助于集成开发环境可以很快地生成程序,程序设计不再是计算机专业人员的专利。很多人认为,只要掌握几种开发工具就可以成为编程高手,其实,这是一种误解。要想成为一个专业的开发人员,至少需要以下三个条件: 1) 能够熟练地选择和设计各种数据结构和算法 2) 至少要能够熟练地掌握一门程序设计语言 3) 熟知所涉及的相关应用领域的知识 其中,后两个条件比较容易实现,而第一个...
金山办公上市,雷军心愿了却!
作者 | 胡巍巍 出品 | CSDN(ID:CSDNnews) 11月17日,大周末的,雷军微博发了个重磅消息: “明天将是里程碑式的一天,金山办公终于成功在科创板挂牌上市了! 从1988年金山创办到今天,WPS走了整整31年。 从1999年以金山办公为主体准备上市算起,这一天,我们等了20年。 WPS和金山的历程,这是一个坚持梦想并最终取得胜利的励志故事。期待大家的祝福!”...
8年经验面试官详解 Java 面试秘诀
作者 |胡书敏 责编 | 刘静 出品 | CSDN(ID:CSDNnews) 本人目前在一家知名外企担任架构师,而且最近八年来,在多家外企和互联网公司担任Java技术面试官,前后累计面试了有两三百位候选人。在本文里,就将结合本人的面试经验,针对Java初学者、Java初级开发和Java开发,给出若干准备简历和准备面试的建议。 Java程序员准备和投递简历的实...
面试官如何考察你的思维方式?
1.两种思维方式在求职面试中,经常会考察这种问题:北京有多少量特斯拉汽车?某胡同口的煎饼摊一年能卖出多少个煎饼?深圳有多少个产品经理?一辆公交车里能装下多少个乒乓球?一个正常成年人有多少根头发?这类估算问题,被称为费米问题,是以科学家费米命名的。为什么面试会问这种问题呢?这类问题能把两类人清楚地区分出来。一类是具有文科思维的人,擅长赞叹和模糊想象,它主要依靠的是人的第一反应和直觉,比如小孩...
17张图带你解析红黑树的原理!保证你能看懂!
二叉查找树 由于红黑树本质上就是一棵二叉查找树,所以在了解红黑树之前,咱们先来看下二叉查找树。 二叉查找树(Binary Search Tree),也称有序二叉树(ordered binary tree),排序二叉树(sorted binary tree),是指一棵空树或者具有下列性质的二叉树: 若任意结点的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若任意结点的...
腾讯“疯狂”开源!
作者 | 马超 责编 | 胡巍巍 出品 | CSDN(ID:CSDNnews) 近日,腾讯自研的万亿级分布式消息中间件TubeMQ正式开源,并捐赠给Apache基金会,成为基金会官方认可的Incubator项目。 我们知道与TubeMQ功能类似的kafka是领英公司在早在10年前捐赠给Apache基金会的金牌项目,而那时的腾讯还在忙于3Q大战,公司文化也相对封闭,甚至连目前社交领...
so easy! 10行代码写个"狗屁不通"文章生成器
前几天,GitHub 有个开源项目特别火,只要输入标题就可以生成一篇长长的文章。 背后实现代码一定很复杂吧,里面一定有很多高深莫测的机器学习等复杂算法 不过,当我看了源代码之后 这程序不到50行 尽管我有多年的Python经验,但我竟然一时也没有看懂 当然啦,原作者也说了,这个代码也是在无聊中诞生的,平时撸码是不写中文变量名的, 中文...
知乎高赞:中国有什么拿得出手的开源软件产品?(整理自本人原创回答)
知乎高赞:中国有什么拿得出手的开源软件产品? 在知乎上,有个问题问“中国有什么拿得出手的开源软件产品(在 GitHub 等社区受欢迎度较好的)?” 事实上,还不少呢~ 本人于2019.7.6进行了较为全面的回答,对这些受欢迎的 Github 开源项目分类整理如下: 分布式计算、云平台相关工具类 1.SkyWalking,作者吴晟、刘浩杨 等等 仓库地址: apache/skywalking 更...
MySQL数据库总结
一、数据库简介 数据库(Database,DB)是按照数据结构来组织,存储和管理数据的仓库。 典型特征:数据的结构化、数据间的共享、减少数据的冗余度,数据的独立性。 关系型数据库:使用关系模型把数据组织到数据表(table)中。现实世界可以用数据来描述。 主流的关系型数据库产品:Oracle(Oracle)、DB2(IBM)、SQL Server(MS)、MySQL(Oracle)。 数据表:数...
相关热词 c# 二进制截断字符串 c#实现窗体设计器 c#检测是否为微信 c# plc s1200 c#里氏转换原则 c# 主界面 c# do loop c#存为组套 模板 c# 停掉协程 c# rgb 读取图片
立即提问