程序设计

专业和发烧友程序员的问答

8
递归列出目录中的所有文件,包括symlink目录中的文件
假设我有一个目录/dir中,其中有3个符号连接其他目录 /dir/dir11,/dir/dir12和/dir/dir13。我想列出中的所有文件dir,包括在那些dir11,dir12和dir13。 为了更通用,我想列出所有文件,包括目录中的符号链接文件。find .,ls -R等停止在符号链接上,而无需导航到它们以进一步列出。
147 linux 

4
正则表达式如何匹配可选字符
我有一个正则表达式,我以为到目前为止可以正常工作。我需要匹配一个可选字符。它可能在那里或可能不在那里。 这是两个字符串。顶部的字符串匹配,而下部的字符串不匹配。低位字符串中没有单个字母是导致失败的原因。 我希望在开头的5位数字后得到单个字母,如果不存在,请继续获取字符串的其余部分。这封信可以A-Z。 如果我([A-Z]{1}) +.*? +从正则表达式中删除,它将匹配我需要的所有内容,但字母除外,但这很重要。 20000 K Q511195DREWBT E00078748521 30000 K601220PLOPOH Z00054878524 这是我正在使用的正则表达式。 /^([0-9]{5})+.*? ([A-Z]{1}) +.*? +([A-Z]{1})([0-9]{3})([0-9]{3})([A-Z]{3})([A-Z]{3}) +([A-Z])[0-9]{3}([0-9]{4})([0-9]{2})([0-9]{2})/
147 regex  string  operators 

7
Django REST框架:向ModelSerializer添加其他字段
我想序列化一个模型,但想包含一个附加字段,该字段要求在要序列化的模型实例上进行一些数据库查找: class FooSerializer(serializers.ModelSerializer): my_field = ... # result of some database queries on the input Foo object class Meta: model = Foo fields = ('id', 'name', 'myfield') 什么是正确的方法?我看到您可以将额外的“上下文”传递给序列化程序,是在上下文字典中传递附加字段的正确答案吗?使用这种方法,获取我需要的字段的逻辑不会与序列化程序定义完全独立,这是理想的,因为每个序列化实例都需要my_field。在DRF序列化程序文档的其他地方,它表示 “额外字段可以对应于模型上的任何属性或可调用的属性”。我所说的是多余的领域吗?我应该在Foo的模型定义中定义一个返回my_field值的函数,并在序列化程序中将my_field连接到该可调用项吗?看起来像什么? 在此先感谢您,如有必要,我们很乐意澄清问题。



8
在Android中的ImageButton中调整图像
我的活动中有6个ImageButton,我通过它们中的代码设置了图像(不使用xml)。 我希望它们覆盖按钮区域的75%。但是,当某些图像覆盖的区域较少时,有些图像太大而无法放入imageButton。如何以编程方式调整大小并显示它们?下面是屏幕截图 下面是xml文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_marginBottom="5sp" android:layout_marginLeft="2sp" android:layout_marginRight="5sp" android:layout_marginTop="0sp" > <LinearLayout android:layout_height="0dp" android:layout_width="match_parent" android:layout_weight="1" android:orientation="horizontal"> <ImageButton android:layout_height="match_parent" android:layout_width="0dp" android:layout_weight="1" android:id="@+id/button_topleft" android:layout_marginBottom="5sp" android:layout_marginLeft="2sp" android:layout_marginRight="5sp" android:layout_marginTop="0sp" /> <ImageButton android:layout_height="match_parent" android:layout_width="0dp" android:layout_weight="1" android:id="@+id/button_topright" android:layout_marginBottom="5sp" android:layout_marginLeft="2sp" android:layout_marginRight="5sp" android:layout_marginTop="0sp" /> </LinearLayout> <LinearLayout android:layout_height="0dp" android:layout_width="match_parent" android:layout_weight="1" android:orientation="horizontal"> <ImageButton android:layout_height="match_parent" android:layout_width="0dp" …

28
如何计算Java中某人的年龄?
我想以Java方法的整数形式返回年龄(以年为单位)。我现在所拥有的是getBirthDate()返回一个Date对象(带有出生日期;-)的以下内容: public int getAge() { long ageInMillis = new Date().getTime() - getBirthDate().getTime(); Date age = new Date(ageInMillis); return age.getYear(); } 但是由于不赞成使用getYear(),所以我想知道是否有更好的方法可以做到这一点?我什至不确定这是否能正常工作,因为我还没有进行单元测试。
147 java  date  calendar 



9
<和之间有什么区别?超级E>和<?扩展E>?
&lt;? super E&gt;和之间有什么区别&lt;? extends E&gt;? 例如,当您查看类时java.util.concurrent.LinkedBlockingQueue,构造函数具有以下签名: public LinkedBlockingQueue(Collection&lt;? extends E&gt; c) 对于方法之一: public int drainTo(Collection&lt;? super E&gt; c)
147 java  generics 

6
从Javascript访问MVC的模型属性
我的视图模型中包含以下模型 public class FloorPlanSettingsModel { public int Id { get; set; } public int? MainFloorPlanId { get; set; } public string ImageDirectory { get; set; } public string ThumbnailDirectory { get; set; } public string IconsDirectory { get; set; } } 如何从Javascript访问上述属性之一? 我尝试了这个,但是我得到了“未定义” var floorplanSettings = "@Model.FloorPlanSettings"; alert(floorplanSettings.IconsDirectory);

14
工具:在Android清单中替换而不是替换
我正在使用具有许多不同库依赖项的gradle项目,并使用新的清单合并。在我的&lt;application /&gt;标签中,我将其设置为: &lt;application tools:replace="android:icon, android:label, android:theme, android:name" android:name="com.example.myapp.MyApplcation" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/application_name" android:logo="@drawable/logo_ab" android:theme="@style/AppTheme" &gt; .... &lt;/application&gt; 但是我收到错误: /android/MyApp/app/src/main/AndroidManifest.xml:29:9 Error: Attribute application@icon value=(@drawable/ic_launcher) from AndroidManifest.xml:29:9 is also present at {Library Name} value=(@drawable/app_icon) Suggestion: add 'tools:replace="android:icon"' to &lt;application&gt; element at AndroidManifest.xml:26:5 to override /android/MyApp/app/src/main/AndroidManifest.xml:30:9 Error: Attribute application@label value=(@string/application_name) from AndroidManifest.xml:30:9 is also …

4
如何使用Spring RestTemplate发布表单数据?
我想将以下(工作)curl代码段转换为RestTemplate调用: curl -i -X POST -d "email=first.last@example.com" https://app.example.com/hr/email 如何正确传递email参数?以下代码导致404 Not Found响应: String url = "https://app.example.com/hr/email"; Map&lt;String, String&gt; params = new HashMap&lt;String, String&gt;(); params.put("email", "first.last@example.com"); RestTemplate restTemplate = new RestTemplate(); ResponseEntity&lt;String&gt; response = restTemplate.postForEntity( url, params, String.class ); 我试图在PostMan中制定正确的调用,并且可以通过在主体中将email参数指定为“ form-data”参数来使其正常工作。在RestTemplate中实现此功能的正确方法是什么?

8
使用Tomcat启动Spring Boot时的用户名和密码是什么?
当我通过Spring Boot和访问权限部署Spring应用程序时,localhost:8080我必须进行身份验证,但是用户名和密码是什么或如何设置呢?我试图将其添加到我的tomcat-users文件中,但是没有用: &lt;role rolename="manager-gui"/&gt; &lt;user username="admin" password="admin" roles="manager-gui"/&gt; 这是应用程序的起点: @SpringBootApplication public class Application extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } } 这是Tomcat依赖项: &lt;dependency&gt; &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt; &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt; &lt;scope&gt;provided&lt;/scope&gt; &lt;/dependency&gt; 我如何认证localhost:8080?

4
抑制makefile中命令调用的回显?
我为分配编写了一个程序,该程序应该将其输出打印到stdout。分配规范要求创建一个Makefile,该文件在被调用make run &gt; outputFile时应运行程序并将输出写入文件,该文件的SHA1指纹与规范中给出的指纹相同。 我的问题是我的makefile: ... run: java myprogram 还将将运行我的程序(例如java myprogram)的命令打印到输出文件,以便我的文件包括多余的行,从而导致指纹错误。 有什么方法可以执行命令而命令调用不回显命令行?
147 linux  unix  makefile  posix 

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.